1.2 KiB
Optimizations
Expr has a bunch of optimization which will produce more optimal program during compile step.
In array
value in ['foo', 'bar', 'baz']
If expr finds an in or not in expression with an array, it will be transformed into:
value in {"foo": true, "bar": true, "baz": true}
Constant folding
Arithmetic expressions with constants is computed on compile step and replaced with result.
-(2-5)**3-2/(+4-3)+-2
Will be compiled to just single number:
23
So in expressions it's safe to use some arithmetics for better readability:
percentage > 0.3 * 100
As it will be simpified to:
percentage > 30
In range
user.Age in 18..32
Will be replaced with binary operator:
18 <= user.Age && user.Age <= 32
not in operator will also work.
Const range
1..10_000
Ranges computed on compile stage, repleced with preallocated slices.
Const expr
If some function marked as constant expression with expr.ConstExpr. It will be replaced with result
of call, if all arguments are constants.
expr.ConstExpt("fib")
fib(42)
Will be replaced with result of fib(42) on compile step. No need to calculate it during runtime.