LEARN VOID / DATA & BEHAVIOR
Nullable values
Represent absence explicitly.
func double(value: i32) -> i32:
return value * 2
func apply<t>(callback: func(t) -> t, value: t) -> t:
return callback(value)
func entry(value: i32) -> i32:
var callback: (func(i32) -> i32)?
if callback is Ok(var absent):
return absent(value)
callback = double
return match callback:
Ok(var present): apply(present, value)
null: -1Input 5Expected result 10
Make absence part of the type
A question mark marks a nullable type. Parentheses distinguish a nullable function value from a function with a nullable result: (func(i32) -> i32)? is a callback that may be null.
Check before using the value
Ok(var present) matches a present nullable value and captures a nonnullable view. This use of Ok does not convert nullable values into result values. null matches absence.
Follow a callback from absent to present
The callback starts absent, then receives double. The successful match passes it to apply and returns 10 for input 5. Invoking a null callback would be a runtime failure; the match makes the intended case explicit.