- You need to switch and pattern-match on the optional value:
switch result {
case (?val) { .../* val : Text here*/... };
case null { ... };
}
If you have multiple of such tests, take a look at the do?
construct, which comes in handy and e.g. allows collapsing multiple switches into one:
let result = do? { List.last(list1)! + List.last(list2)! + 1 };
switch result {
case (?n) { ... handle result ... }
case null { ... handle error ... }
}
The !
operator inside a do?
block converts ?T
to T
and returns null
to the enclosing do?
otherwise.
- There are library functions for pretty-printing values of number types:
import Nat "mo:base/Nat";
let str = Nat.toText(1);