In order to make working with optional types easier without losing the information that we are using optionals, we want to introduce a Conditional Access Operator.
Null conditional operator: (x?.field or x?.method(...))
- Used to call a method or load a field on an optional value
- Can be used only on optional values (T?), usage on other types is an error
- Always returns an optional - null if the value if null, the result of the method call / field otherwise
- If the resulting value is optional, it is flattened - there are no doubly optionals
- Not supported for operators, including [ ] and ( )
- No short-circuit, if the operand before the
?. is null, any chained calls are stil evaluated.
a?.getB().c would error unless B? had a member c
- Uses the members from the base-type (T if invoked on T?)
Acceptance Criteria:
In order to make working with optional types easier without losing the information that we are using optionals, we want to introduce a Conditional Access Operator.
Null conditional operator: (x?.field or x?.method(...))
?.is null, any chained calls are stil evaluated.a?.getB().cwould error unlessB?had a membercAcceptance Criteria:
Simple test case with null- and non-null-value
?.flattens optional return type"Collision:" Member exists on both Nullable-type and on base-type, which one does the null-safe operator use?
Properly converts back and forth between
nulland-1fornull as usizeIf decision on member-get and member-set operator: Tests for that
Fails for non-optional type
Operator cannot be overridden in a custom type