In the previous pattern for loops (Returning objects from patterns),
you might have noticed that the after property and condition property can be null,
depending on the type of loop that is matched. In fact, we say that these properties are nullable.
In CodeXM documentation, a nullable value is indicated by the potential type followed by a question mark;
for example, string?.
Accessing a null value is not a fatal run-time error in CodeXM, as it is in many other languages.
A nullable property is one that might not return an actual value: This does require additional CodeXM code to account for that possibility.
You can handle a nullable type in one of the following ways:
-
Check that the value is not
nullby matching it against the built-in patternNonNull. As in all pattern matching, you can use theasclause to extract the matched value. You can also match againstnullitself, then if the match is true, execute code that doesn't depend on the property having a value.Use the matching method when you want to act only on non-
nullvalues and ignorenullvalues altogether. -
Use the null-coalescing operator
??to provide a default value that stands in for anullproperty.Use the coalescing (default value) method when you want to act on all possible values, including
null.
The following code sample uses the method of matching NonNull:
for loop in globalset allFunctionCode % allLoops
where loop.after matches NonNull as aft
&& // Use aft here, knowing now that it isn't null.
The following code sample uses the converse method of checking that a property value is null:
for loop in globalset allFunctionCode % allLoops
where loop.after == null
&& ( /* The loop.after property is now confirmed to be null,
so don't attempt to use its value here! */ )
The null-coalescing operator simply specifies a default value to use when a property is null.
The following code fragment shows how to use it in code:
myNullableString ?? "default value"
... After this expression, the CodeXM code can now access myNullableString just as it accesses any non-nullable property.
If the property has a value, that is what CodeXM inspects. If the property is null, CodeXM uses the value
"default value" instead.