as_option<T extends CandidType> static method

Option<T> as_option<T extends CandidType>(
  1. CandidType option
)

Useful when deserializing an Option Response but working with Candid's subtyping rules. Candid's rules state that an Option with a non-null value can be sent as the value itself without the Option wrapping it. This function makes that check for you and returns the Option type even if the value is sent by itself for the consistency.

Uint8List candidbytes = ...;
Option<Nat> optional_nat = CandidType.as_option<Nat>(c_backwards_one(candidbytes));

Implementation

static Option<T> as_option<T extends CandidType>(CandidType option) {
    if (option is Option) {
        return option.cast_option<T>();
    } else {
        return Option<T>(value: option as T);
    }
}