- ?
- Nil
Type or Nil
- A question mark ? after the type in the function declaration is the same as accepting nil as well.
- Int32? is the same as Int32 | ::Nil
examples/functions/type_or_nil.cr
def func(number : Int32?) puts number end func(23) func(nil) # func() # Error: wrong number of arguments for 'func' (given 0, expected 1) # func(2.3) # Error: no overload matches 'func' with type Float64 def show(number : Int32) puts number end show(23) # show(nil) # Error: no overload matches 'show' with type Nil def other(number : Int32 | ::Nil) puts number end other(23) other(nil)