Member Access

A dot (.) is used to access a Member of a Type, such as accessing the value of a Field or Property, or call a Method:

var x := lMyObject.SomeProperty;

or

lMyObject.DoSomething(5);

Member access requires an Expression on the left side of the dot that is not nil. If the expression evaluates to nil, a NullReferenceException will occur.

The right side of the dot needs to have one of the following expressions:

which is expressed by the name of a Member of that type that is visible from the current scope.

The resulting expression is of the same type as the member that is being accessed, and it can be used in a more complex expression, or – if the member is a non-read-only Field, Propertiy or Event – be assigned to, to update their value.

var x := lMyList.Count + 5;

(↑ Using the result of a property access as part of a + Binary Expression.)

lMyObject.FooBar := 'Baz';

(↑ Assigning a value to a property access expression.)

Nil-safe Member Access

By default, member access throws a NullReferenceException (NRE) if the left side is a nil reference (e.g an uninitialized variable). Using the "colon" (:) operator, also called the "not-nil" operator, instead of the dot (.) can 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 at all.

var x := MyClass:SomeProperty;

This extends to any further member access chained to the expression. In the example below, the last two method calls do not need the : operator, to protect against a nil value in SomeProperty. (However, if SomeProperty is assigned but CalculateSomething returns nil, the call to ToString would still NRE.)

var x := MyClass:SomeProperty.CalculateSomething.ToString;

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 nil is still nil...

See Also