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.GroupType

Remember 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.

Note: Technically speaking, property access from a dynamic 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.P3

By using null-safe paths, Gosu returns null if any of the following are null:

  • a
  • a.P1
  • a.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.

Warning: In Studio, Gosu code in the Gosu Scratchpad has different compiler behavior than any other part of Studio. If you run code in Gosu Scratchpad using the Run or Debug buttons, then the code compiles and runs locally in Studio with different behavior. Most notably, the behavior of null safety for properties in the period operator is different. In nearly all other contexts in Studio, the period operator is null safe for properties. However, in Gosu Scratchpad in Studio, Gosu is not null-safe. Code that might not throw an exception in a Gosu class might throw an exception in Gosu Scratchpad. If Studio is connected to a server running with a DCEVM and you click Run in Debug Process , then Studio sends the Gosu code to the server. The PolicyCenter server uses the standard null-safe behavior for the period operator.

See also