.. _stdlib_option: ==================================================== Monadic Option — a value-or-nothing tagged pair ==================================================== .. das:module:: option Monadic ``Option`` — represents a value that may or may not be present. Functional API for modelling "absence" in ordinary value code, where nullable pointers are inapplicable and sentinel values (``-1``, ``""``) are fragile. The payload may be any type, including non-copyable ones (``array``, ``table``, lambdas): constructors and combinators clone or move on the non-copyable branch via ``static_if (typeinfo can_copy(...))``. Construct via ``some(v)`` (clones), ``move_some(v)`` (moves out of a mutable source), or ``none(type)``; read via ``unwrap`` / ``unwrap_or`` / ``??``; compose via ``map`` / ``and_then`` / ``filter`` / ``or_else`` through the ``|>`` pipe. See also :ref:`tutorial_option_and_result` and ``daslib/result`` for the ``Result`` counterpart. ++++++++++ Structures ++++++++++ .. _struct-option-Option: .. das:attribute:: Option struct Option ++++++++++++ Constructors ++++++++++++ * :ref:`move_some (var v: auto(TT)) : auto ` * :ref:`none (t: auto(TT)) : auto ` * :ref:`some (v: auto(TT)) : auto ` .. _function-option_move_some_autoTT_0x39: .. das:function:: move_some(v: auto(TT)) : auto def move_some (var v: auto(TT)) : auto :Arguments: * **v** : auto(TT) .. _function-option_none_autoTT_0x42: .. das:function:: none(t: auto(TT)) : auto def none (t: auto(TT)) : auto :Arguments: * **t** : auto(TT) .. _function-option_some_autoTT_0x27: .. das:function:: some(v: auto(TT)) : auto def some (v: auto(TT)) : auto :Arguments: * **v** : auto(TT) +++++++ Queries +++++++ * :ref:`is_none (o: $Option(type\)) : bool ` * :ref:`is_some (o: $Option(type\)) : bool ` .. _function-option_is_none__builtin_Optiontype_ls_autoTT_gr_: .. das:function:: is_none(o: $Option(type)) : bool def is_none (o: $Option(type)) : bool :Arguments: * **o** : typemacro .. _function-option_is_some__builtin_Optiontype_ls_autoTT_gr_: .. das:function:: is_some(o: $Option(type)) : bool def is_some (o: $Option(type)) : bool :Arguments: * **o** : typemacro ++++++++++++ Transforming ++++++++++++ * :ref:`and_then (o: $Option(type\); f: block\<(v:TT):$Option(type\)\>) : auto ` * :ref:`filter (o: $Option(type\); p: block\<(v:TT):bool\>) : auto ` * :ref:`map (o: $Option(type\); f: block\<(v:TT):auto(UU)\>) : auto ` * :ref:`or_else (o: $Option(type\); f: block\<():void\>) : auto ` * :ref:`or_value (o: $Option(type\); v: TT) : auto ` .. _function-option_and_then__builtin_Optiontype_ls_autoTT_gr__block_ls_v_c_TT_c__builtin_Optiontype_ls_autoUU_gr__gr_: .. das:function:: and_then(o: $Option(type); f: block<(v:TT):$Option(type)>) : auto def and_then (o: $Option(type); f: block<(v:TT):$Option(type)>) : auto :Arguments: * **o** : typemacro * **f** : block<(v:TT):typemacro> .. _function-option_filter__builtin_Optiontype_ls_autoTT_gr__block_ls_v_c_TT_c_bool_gr_: .. das:function:: filter(o: $Option(type); p: block<(v:TT):bool>) : auto def filter (o: $Option(type); p: block<(v:TT):bool>) : auto :Arguments: * **o** : typemacro * **p** : block<(v:TT):bool> .. _function-option_map__builtin_Optiontype_ls_autoTT_gr__block_ls_v_c_TT_c_autoUU_gr_: .. das:function:: map(o: $Option(type); f: block<(v:TT):auto(UU)>) : auto def map (o: $Option(type); f: block<(v:TT):auto(UU)>) : auto :Arguments: * **o** : typemacro * **f** : block<(v:TT):auto(UU)> .. _function-option_or_else__builtin_Optiontype_ls_autoTT_gr__block_ls__c_void_gr_: .. das:function:: or_else(o: $Option(type); f: block<():void>) : auto def or_else (o: $Option(type); f: block<():void>) : auto :Arguments: * **o** : typemacro * **f** : block .. _function-option_or_value__builtin_Optiontype_ls_autoTT_gr__TT: .. das:function:: or_value(o: $Option(type); v: TT) : auto def or_value (o: $Option(type); v: TT) : auto :Arguments: * **o** : typemacro * **v** : TT ++++++++++ Extraction ++++++++++ * :ref:`expect_value (o: $Option(type\); msg: string) : TT ` * :ref:`move_unwrap (var o: $Option(type\)) : TT ` * :ref:`unwrap (o: $Option(type\)) : TT ` * :ref:`unwrap_or (o: $Option(type\); d: TT) : TT ` * :ref:`unwrap_or_default (o: $Option(type\)) : TT ` * :ref:`unwrap_or_else (o: $Option(type\); f: block\<():void\>) : TT ` .. _function-option_expect_value__builtin_Optiontype_ls_autoTT_gr__string: .. das:function:: expect_value(o: $Option(type); msg: string) : TT def expect_value (o: $Option(type); msg: string) : TT :Arguments: * **o** : typemacro * **msg** : string .. _function-option_move_unwrap__builtin_Optiontype_ls_autoTT_gr_: .. das:function:: move_unwrap(o: $Option(type)) : TT def move_unwrap (var o: $Option(type)) : TT :Arguments: * **o** : typemacro .. _function-option_unwrap__builtin_Optiontype_ls_autoTT_gr_: .. das:function:: unwrap(o: $Option(type)) : TT def unwrap (o: $Option(type)) : TT :Arguments: * **o** : typemacro .. _function-option_unwrap_or__builtin_Optiontype_ls_autoTT_gr__TT: .. das:function:: unwrap_or(o: $Option(type); d: TT) : TT def unwrap_or (o: $Option(type); d: TT) : TT :Arguments: * **o** : typemacro * **d** : TT .. _function-option_unwrap_or_default__builtin_Optiontype_ls_autoTT_gr_: .. das:function:: unwrap_or_default(o: $Option(type)) : TT def unwrap_or_default (o: $Option(type)) : TT :Arguments: * **o** : typemacro .. _function-option_unwrap_or_else__builtin_Optiontype_ls_autoTT_gr__block_ls__c_void_gr_: .. das:function:: unwrap_or_else(o: $Option(type); f: block<():void>) : TT def unwrap_or_else (o: $Option(type); f: block<():void>) : TT :Arguments: * **o** : typemacro * **f** : block ++++++++++++ Side effects ++++++++++++ * :ref:`if_none (o: $Option(type\); f: block\<():void\>) ` * :ref:`if_some (o: $Option(type\); f: block\<(v:TT):void\>) ` .. _function-option_if_none__builtin_Optiontype_ls_autoTT_gr__block_ls__c_void_gr_: .. das:function:: if_none(o: $Option(type); f: block<():void>) def if_none (o: $Option(type); f: block<():void>) :Arguments: * **o** : typemacro * **f** : block .. _function-option_if_some__builtin_Optiontype_ls_autoTT_gr__block_ls_v_c_TT_c_void_gr_: .. das:function:: if_some(o: $Option(type); f: block<(v:TT):void>) def if_some (o: $Option(type); f: block<(v:TT):void>) :Arguments: * **o** : typemacro * **f** : block<(v:TT):void> +++++++ Pairing +++++++ * :ref:`zip (a: $Option(type\); b: $Option(type\)) : auto ` .. _function-option_zip__builtin_Optiontype_ls_autoTT_gr___builtin_Optiontype_ls_autoUU_gr_: .. das:function:: zip(a: $Option(type); b: $Option(type)) : auto def zip (a: $Option(type); b: $Option(type)) : auto :Arguments: * **a** : typemacro * **b** : typemacro +++++++++ Operators +++++++++ * :ref:`$Option(type\)\!= (o: $Option(type\); v: TT) : bool ` * :ref:`$Option(type\)== (a: $Option(type\); b: $Option(type\)) : bool ` * :ref:`$Option(type\)== (o: $Option(type\); v: TT) : bool ` * :ref:`$Option(type\)?? (o: $Option(type\); d: TT) : TT ` * :ref:`auto(TT)\!= (v: auto(TT); o: $Option(type\)) : bool ` * :ref:`auto(TT)== (v: auto(TT); o: $Option(type\)) : bool ` .. _function-option__ex__eq___builtin_Optiontype_ls_autoTT_gr__TT: .. das:function:: $Option(type)!=(o: $Option(type); v: TT) : bool def $Option(type)!= (o: $Option(type); v: TT) : bool :Arguments: * **o** : typemacro * **v** : TT $Option(type)== ^^^^^^^^^^^^^^^^^^^^^^^^^ .. _function-option__eq__eq___builtin_Optiontype_ls_autoTT_gr___builtin_Optiontype_ls_autoTT_gr_: .. das:function:: $Option(type)==(a: $Option(type); b: $Option(type)) : bool def $Option(type)== (a: $Option(type); b: $Option(type)) : bool :Arguments: * **a** : typemacro implicit * **b** : typemacro implicit .. _function-option__eq__eq___builtin_Optiontype_ls_autoTT_gr__TT: .. das:function:: $Option(type)==(o: $Option(type); v: TT) : bool ---- .. _function-option__q__q___builtin_Optiontype_ls_autoTT_gr__TT: .. das:function:: $Option(type)??(o: $Option(type); d: TT) : TT def $Option(type)?? (o: $Option(type); d: TT) : TT :Arguments: * **o** : typemacro * **d** : TT .. _function-option__ex__eq__autoTT__builtin_Optiontype_ls_TT_gr__0x10a: .. das:function:: auto(TT)!=(v: auto(TT); o: $Option(type)) : bool def auto(TT)!= (v: auto(TT); o: $Option(type)) : bool :Arguments: * **v** : auto(TT) * **o** : typemacro .. _function-option__eq__eq__autoTT__builtin_Optiontype_ls_TT_gr__0x102: .. das:function:: auto(TT)==(v: auto(TT); o: $Option(type)) : bool def auto(TT)== (v: auto(TT); o: $Option(type)) : bool :Arguments: * **v** : auto(TT) * **o** : typemacro