NFun icon indicating copy to clipboard operation
NFun copied to clipboard

Support optional type

Open tmteam opened this issue 3 years ago • 0 comments

type optional-<T>- : T | none

  1. assume we have 'none' constant of type 'NONE'
  2. optional type T is type that can be either T or NONE
  3. T can be converted to optional-<T>-
  4. default of optional-<T>- equals 'none' optional:
  5. '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

tmteam avatar May 30 '22 13:05 tmteam