@NathanosDev I want to do the following assign in Motoko:
let a: ?Nat = ?3;
let b: Nat = a; <----Error: Cannot produce expected type
How can I do that assignement?
@NathanosDev I want to do the following assign in Motoko:
let a: ?Nat = ?3;
let b: Nat = a; <----Error: Cannot produce expected type
How can I do that assignement?
You can unwrap the value: Option | Internet Computer
it says " @deprecated Option.unwrap is unsafe and fails if the argument is null; it will be removed soon; use a switch
or do?
expression instead". How can I use switch
?
You can do:
let a: ?Nat = ?3;
let b: ?Nat = a;
or unwrap it like this in one line
let a: ?Nat = ?3;
let ?b = a else return; // or 0 etc if outside function
That will then let you access unwrapped a
as b
let ?b = a else return #err("item was null");
← Note, the else has to return a “none” which can be a return, a trap, or a default value.
or
let b = switch(a){
case(null) 0; //default
case(?val) val;
};