If x is null or a non-interrupting error, then y is returned. When it is an error, the error message is suppressed like it is with try.
|
|
In all other cases, x is returned.
|
Note that y is lazily evaluated, that is, it is only evaluated if it is necessary.
The null coalescing operator can be combined with augmented assignment as a shortcut for if x === null then x = y
and if not x#?i then x#i = y
.
|
|
|
|
|
|
|
|
It is also possible to install a method for a particular type to determine whether an instance x of that type should be considered "null" or not. Such a method should return either x or null
|
|
|
|
|
|
It is also possible to use ?? as a prefix operator to call this method.
|
|
|
Although this operator is "flexible" in the sense that it possible to install a method to determine the behavior on the left-hand side, it is not truly flexible like most other binary operators. In particular, since it is only lazily evaluated, it is not possible to install a method that uses the value of the right-hand side.
This operator may be used as a binary operator in an expression like x??y.
This operator may be used as a prefix unary operator in an expression like ??y. The user may install a method for handling such expressions with code such as
?? Y := (y) -> ...
where Y is the class of y.