Versions/v1.2.0-alpha.06/English

Using Option

Sections1
On This Page1
API Symbols4

Using Option

The Option type was introduced earlier, defining its characteristics. Since the Option type can represent both a value and the absence of a value (where the absence may be interpreted as an error in certain contexts), it can also be used for error handling.

For example, in the following case, if the parameter value of the function getOrThrow equals Some(v), it returns the value v; if the parameter equals None, it throws an exception.

Because Option is an extremely common type, Cangjie provides multiple destructuring methods to facilitate its usage, including: pattern matching, the getOrThrow function, the coalescing operator (??), and the question mark operator (?). Each of these methods will be explained in detail below.

- Pattern Matching: Since the Option type is an enum type, the pattern matching for enums mentioned earlier can be used to destructure Option values. For example, in the following function getString, which takes a parameter of type ?Int64, if the parameter is a Some value, it returns the string representation of the contained number; if the parameter is None, it returns the string "none".

The execution result of the above code is:

- Coalescing Operator (??): For an expression e1 of type ?T, if you want to return a value e2 of type T when e1 equals None, you can use the ?? operator. For the expression e1 ?? e2, if e1 equals Some(v), it returns v; otherwise, it returns e2. Example:

The execution result of the above code is:

- Question Mark Operator (?): The ? operator must be used in conjunction with ., (), [], or {} (specifically in trailing lambda calls) to enable support for these operations on Option types. Taking . as an example (similarly for (), [], and {}), for an expression e of type ?T1, when e equals Some(v), the value of e?.b is Option.Some(v.b); otherwise, it is Option.None, where T2 is the type of v.b. Example:

The question mark operator (?) supports multi-level access. For example, in a?.b.c?.d (similarly for (), [], and {}), the expression a must be of type Option, where T1 contains an instance member b. The type of b must contain an instance member c, and c must be of type Option, where T2 contains an instance member d. The type of a?.b.c?.d is Option, where T3 is the type of T2's instance member d. When a equals Some(va) and va.b.c equals Some(vc), the value of a?.b.c?.d is Option.Some(vc.d). When a equals Some(va) but va.b.c equals None, the value is Option.None (d is not evaluated). When a equals None, the value is Option.None (b, c, and d are not evaluated).

- getOrThrow Function: For an expression e of type ?T, you can destructure it by calling the getOrThrow function. When e equals Some(v), getOrThrow() returns v; otherwise, it throws an exception. Example:

The execution result of the above code is:

cangjie
func getOrThrow(a: ?Int64) {
    match (a) {
        case Some(v) => v
        case None => throw NoneValueException()
    }
}