var x := lMyObject.SomeProperty;
The right side if the dot needs to have one of thew following expressions:
which is expressed by the name of a Members 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.
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
: 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
nil, the call to
ToString wukd 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
nil, since according to nullable boolean logic, 'lFile.Exists' would be
nil, and since any operation involving
not nil is still