static()

The static() function wraps a boolean expression and forces it to be evaluated at compile-time rather than at runtime. Like its defined() and exists() counter-parts, code that cannot be reached due to a static() condition being false will be skipped by the compiler and eliminated from the executable.

If the expression wrapped in static() cannot be evaluated at compile-time (for example because it contains non-constant elements), compilation will fail with an error.

const
  version = 10;

...

if static(version < 10) then
  ThisCode.WillNotBeCompiled() // unknown identifiers here will also be ignored
else
  writeLn('Version was higher than 10');
public const int version = 10;

...

if (@static(version < 10))
  ThisCode.WillNotBeCompiled(); // unknown identifiers here will also be ignored
else
  writeLn("Version was higher than 10");
let version = 10;

...

if `static`(version < 10) {
  ThisCode.WillNotBeCompiled() // unknown identifiers here will also be ignored
} else {
  writeLn("Version was higher than 10");
}
public const int version = 10;

...

if (@static(version < 10))
  ThisCode.WillNotBeCompiled(); // unknown identifiers here will also be ignored
else
  writeLn("Version was higher than 10");

In the above example, the compiler would ignore the first leg of the if clause completely, because the static condition is not met. It would merely emit the (unconditional) writeL() call.

Like defined() and exists(), calls to static() can be combined with "normal" at-runtime if conditions.

Using static() outside of if

Note that while exists() can be called as a function in any context, it will only directly influence compiler behavior when used inside the expression of an if statement. For example, when assigning the result of a static() call to a variable and then using that variable in an if clause, the compiler would initiate the variable with compile-time evaluated static value, but would compile the if clause as usual, to be evaluated at run-time.

See Also