44. Constant expression checker and substitution¶
The constant_expression module implements constant expression function argument check, as well as argument substitution.
All functions and symbols are in “constexpr” module, use require to get access to it.
require daslib/constant_expression
44.1. Function annotations¶
-
constexpr
¶
This macro implements a constexpr function argument checker. Given list of arguments to verify, it will fail for every one where non-constant expression is passed. For example:
[constexpr (a)]
def foo ( t:string; a : int )
print("{t} = {a}\n")
var BOO = 13
[export]
def main
foo("blah", 1)
foo("ouch", BOO) // comilation error: `a is not a constexpr, BOO`
-
constant_expression
¶
This function annotation implments constant expression folding for the given arguments. When argument is specified in the annotation, and is passed as a contstant expression, custom version of the function is generated, and an argument is substituted with a constant value. This allows using of static_if expression on the said arguments, as well as other optimizations. For example:
[constant_expression(constString)]
def take_const_arg(constString:string)
print("constant string is = {constString}\n") // note - constString here is not an argument