Member Access Expressions

A dot is used to access a members of a type, such as fields, properties or methods.

var x := MyClass.SomeProperty;

Nil-safe Member Access

By default, member access throws a NullReferenceException if the left side is a nil reference (e.g an uninitialized variable). The "colon" (:) operator, also called the "not-nil" operator, can be used to avoid that.

Nil-safe member access works like regular member access with ., except that if the left hand side is nil, nothing on the right side is evaluated and instead the entire expression resolves to nil. The : operator is right-associative, meaning that if the left side is nil, all of the right side of the expression is skipped, and not evaluated.

var x := MyClass:SomeProperty;

Note that if the type of the right-hand expression is a value type (such as an Integer or a Boolean) that normally cannot be nil, the combined expression will be treated as a nullable version of that value type, according to Nullabilty rules.

var b := MyClass:SomeBooleanProperty; // b is a "nullable Boolean"

Note that this can have side effects depending on how the result is used, especially if a nullable Boolean result is used as part of a more complex expression.

var lFile := "/Some/File.exe";
//...
if not lFile:Exists then // this might not do what you think it does, if lFile is nil.

The above code will not do what you might expect from it, if lFile is nil, since according to nullable boolean logic, 'lFile.Exists' would be nil, and since any operation involving nil remains nil, not lFile:FileExists is still nil...

See Also

  • the "Elvis" operator, ?. in C# and Swift.