How the standard period operator handles null
The standard period (.) operator has a special behavior that some languages do not have. If any object property in a property path expression evaluates to null, in Gosu the entire path evaluates to null. A null value at any location in an object path short-circuits evaluation and results in a null value, with no exception being thrown. This feature is called null-safe short circuiting for a property path expression.
For example, suppose that you have an expression similar to the following:
var groupType = claim.AssignedGroup.GroupTypeRemember
that if any element in the path evaluates to null, the entire expression evaluates
to null. If claim is null, the result is null. Also, if claim.AssignedGroup is null, the result is null.
If the expression
contains a method call, the rules are different. The period operator
does not default to null if the left side of the period is null. With the standard period
operator, if the value on the left of the period is null, Gosu throws a null pointer
exception (NullPointerException).
In other words, method calls are not null-safe because the method could
have side effects.
property get function could generate
side effects, but this is strongly discouraged. If a property accessor
has any side effects, convert the accessor into a method.Example 1
Suppose that you have an expression similar to the following:
claim.AssignedGroup.addEvent("abc")
In this case, if either claim or claim.AssignedGroup
evaluate to null, Gosu throws a NullPointerException. The method call follows
the null value.
Example 2
Suppose that you have an expression similar to the following:
claimant.getSpecialContactRelationship().Contact.Name
If Contact is null, the expression evaluates to
null. Similarly, if getSpecialContactRelationship() evaluates to
null, the expression evaluates to null.
However, remember that evaluation in the expression is left-to-right. If
claimant is null, the expression throws an exception because Gosu cannot
call a method on null.
Example 3
For those cases in which Gosu expects a Boolean value (for example, in an
if statement), a null value coerces to false in Gosu. This
is true regardless of whether the expression’s value was short-circuited. For example, the following
if statement prints “Benefits decision not made yet”, even if claim or
claim.BenefitsStatusDcsn is null:
if( not claim.BenefitsStatusDcsn ) {
print( "Benefits decision not made yet" )
}
Primitives and null-safe paths
Gosu null-safety for property paths does not work if the type of the entire expression is a Gosu primitive type. This is equivalent to saying it does not work if the type of the last item in the series of property accesses has a primitive type. For example, suppose you use the property path:
a.P1.P2.P3By using
null-safe paths, Gosu returns null
if any of the following are null:
aa.P1a.P1.P2
However, if the type of the
P3 property is int or char or another primitive type,
then the expression a.P1.P2.P3
is not null safe.
Primitive types (in contrast to object types, which are descendents of
Object) can never contain the value null.
Thus, Gosu cannot return null from that expression, and any casting
from null to the primitive type would be meaningless. Therefore,
Gosu throws a null pointer exception in those conditions.
See also
