Skip to content

Compile Time constants

Pieter De Rycke edited this page Aug 4, 2019 · 5 revisions

Variables as defined in a formula can be replaced by a constant value at compile time. This feature is useful in case that a number of the parameters don't frequently change and that the formula needs to be executed many times. In this case, you replace a number of the parameters by compile constants: this allows the dynamic compiler to optimize the generated MSIL compared to using a variable and providing the value at execution time.

Option 1: Replacing the variables by constants using a dictionary:

CalculationEngine engine = new CalculationEngine();
var formula = engine.Build("a + b", new Dictionary<string, double>{{"b", 1.0}});

// result will be 4.0
double result = formula(new Dictionary<string, double>{{"a", 3.0 }});

Option 2: Replacing the variables by constants using the "Constant" Method:

CalculationEngine engine = new CalculationEngine();
Func<double, double> formula = (Func<double, double>)engine.Formula("a + b")
    .Parameter("a", DataType.FloatingPoint)
    .Constant("b", 1.0)
    .Result(DataType.FloatingPoint)
    .Build();

// result will be 4.0
double result = formula(3.0);