NFun
NFun copied to clipboard
Support optional type
type optional-<T>- : T | none
- assume we have 'none' constant of type 'NONE'
- optional type T is type that can be either T or NONE
- T can be converted to optional-<T>-
- default of optional-<T>- equals 'none' optional:
- 'any' is nullable by its nature (???)
use 'none' const and aliasless 'none' type with default value of none
a = none
b = if(a == none) "nothing" else "anything" #nothing
#optional-<T>- type can be converted to any. T can be converted to optional-<T>- type
a = if(true) 42 else none # a is optional-<T>-
b:any = a # is ok
c:int = a # fails
#optional can be used only in if expression
a = if(true) 42 else none
b = if(a==none) default else a+31
c = if(a!=none) a+31 else default
'otherwise' function can be used to return fallback value if the origin value is in fallback
#sketch
otherwise(a, defaultValue) =
if(a == none) defaultValue
else a
#sketch
otherwise(a, defaultValue) =
if(a == none) default
else a.notNone()
#sketch
notNone(a) =
if(a == none) error
else a