assigned()

The assigned() function is really just a nice way to check a value against nil (Oxygene and Swift) or null (C#), and performs the same functionality as using the <>, or != operator for comparing the value.

Syntax:

method assigned(expression: Object): Boolean;
bool assigned(object expression);
func assigned(_ expression: Any?) -> Bool
bool assigned(object expression);

Example

As you can see in the sample below, both ways to check against nil work exactly the same. But using assigned can reduce some optical noise – especially in Oxygene where operator precedence requires parenthesis if multiple comparisons are combined with and or or.

var someValue := GetSomeValue();

if someValue <> nil then
  ...
if assigned(someValue) then
  ...
var someValue = GetSomeValue();

if (someValue != null)
{
  ...
}
if (assigned(someValue))
{
  ...
}
var someValue = GetSomeValue()

if someValue <> nil {
  ...
}
if assigned(someValue) {
  ...
}
var someValue = GetSomeValue();

if (someValue != null) {
  ...
}
if (assigned(someValue)) {
  ...
}

Futures

Note that if assigned() is called on a variable of Future Type, it will return whether the result of that future is assigned or not, not whether the future itself is assigned. This is done to preserve the concept that future type variables can be used interchangeably with non-future values.

Use futureAssigned() to check if a future is assigned or not.

For example:

property value: future String read nil; // declared somewhere

...
var x: future String := async nil;
var y: future String := async "Slow to Calculate";
var z: future String {:= nil};

if assigned(x) then
  // waits for the future to calculate, if necessary, and the checks if its nil or has a value.
  // which will be nil, so this block will not run
  ...
if assigned(y) then
  // waits for the future to calculate, if necessary, and the checks if its nil or has a value
  // which will be "Slow to Calculate", so this block will run
  ...
if assigned(z) then
  // has no future to wait for, so it will be false
  // this block will not run
  ...

if futureAssigned(x) then // check if x contains any future at all. it does.
  // this runs
  ...
if futureAssigned(y) then // check if y contains any future at all. it does.
  // this runs
  ...
if futureAssigned(z) then // check if y contains any future at all. it does not.
  // this will not run
  ...

to do: translate to all 4 languages

Future Types are available in Oxygene only.