Discardable Expressions

A discardable is a special kind of expression that can be used as the target of an Assign Statement in order to ignore (part of) the result of the right-hand side of the assignment.

In general most Oxygene Expressions can be used as Statements, meaning they can be used stand-alone, ignoring a possible result. For example, a method can be called, and its result can simply be ignored, by putting the method call as a plain standalone statement. However there are some cases where this is not allowed, and the result of an expression must be reused or acted upon. For example, it is not allowed cannot access a property without doing something with its value.

The discardable expression provides a workaround for those scenarios, by allowing to explicitly assign the result of an express "to nowhere". This is done by using the nil keyword as the left-hand side of an Assign Statement.

There are three commonly useful scenarios for this:

  • Calling into a property to trigger side-effects of its getter (for example, lazy initialization) w/o using it's value
  • Calling a method that is explicitly marked with the WarnUnusedResult Aspect
  • Discarding parts of a Tuple Types result, while extracting others.

For example:

nil := MyManagerClass.Instance; // make sure the Instance is created, but don't use it.
(a, nil, c) := SomeTupleWithThreeValues; // discard the middle value of a tuple 

On the case of tuple expansion, the nil keyword can also be use din combination with Var Statement, to declare new variables to hold some tuple values, while discarding others:

var (a, nil, c) := SomeTupleWithThreeValues; // discard the middle value of a tuple, declares `a` & `c` fresh

See Also