Ifdef statement, else expression

Ifdef statement

The Lava ifdef branching statement, like the Lava else expression (see below), can be used (and LavaPE enforces their use!) to secure assignments to mandatory variables if the assignment source is an optional variable. Example:

function f1
inputs:
  Integer i1, i2; the color of i1 means "optional"
outputs:
  Integer o1;

ifdef
  i1
then
  set o1 <== i1
  this optional-to-mandatory assignment would be rejected by LavaPE
  if it were not secured by ifdef
else
  set o1 <== i2
#ifdef

The then and else clauses of ifdef are both optional, as the their color indicates, but at least one of them must be present.

Please note that Lava performs comprehensive initialization checks for all kinds of variables. This may particularly involve a check whether a value is assigned to a certain variable in all branches of a Lava exec, or in all preceding branches before a read reference to that variable. These checks will succeed only if you don't distribute these assignments over separate ifdef statements as in the following, modified example:

function f1
inputs:
  Integer i1, i2; the color of i1 means "optional"
outputs:
  Integer o1;

ifdef
  i1
then
  set o1 <== i1
#ifdef
ifdef
  i1
else
  set o1 <== i2
#ifdef
Here LavaPE would blame that the mandatory output parameter o1
isn't set in all branches of funktion f1...
... but this would be correct:
ifdef
  i1
then
  set o1 <== i1
else
  set o1 <== i2
#ifdef

Else expression

The else expression combines two (or a sequence of) expressions in such a way that their values are tested (at run time) from left to right for being non-null (i.e., != Ø). The first non-null value is taken as the result of the compound else expression.

LavaPE reports an error (i.e., at programming time!) if the result of an else expression is assigned to a mandatory variable but none of the component expressions yields a mandatory result. An error is reported also if the else expression is unnecessary since already its first operand is mandatory.

See also

Comprehensive initialization checks