Property accessor paths are null safe

One notable difference between Gosu and some other languages is that property accessor paths in Gosu are tolerant of unexpected null values. This feature affects only expressions separated by period characters that access a series of instance variables or properties such as:

obj.Property1.Property2.Property3

In most cases, if any object to the left of the period character is null, Gosu does not throw a null pointer exception (NPE) and the expression returns null. Gosu null-safe property paths tend to simplify real-world code. Gosu null-tolerant property accessor paths are a very good reason to expose data as properties in Gosu classes and interfaces rather than as setter and getter methods.

Gosu provides additional null-safe operators. For example, specify default values with code like:

// Set display text to the String in the txt variable, or if it is null use "(empty)"
var displayText = txt ?: "(empty)"
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