Eval()

Format

Eval(String[, ParamsArray])

Returns

Evaluates the specified expression. Optionally it can also override the value of context parameters for the duration of the evaluation.

Behaves in exactly the same way as the EvalExp() function, except that the first parameter’s type is a single value string and not an expression. 

Use caution when nesting the Eval() functions, as they can quickly become complicated and difficult to troubleshoot.

Inputs

String: The string to be evaluated.
Behaviour: Required
Dimension: SingleValue
Note: Also supports a single string that contains an expression which returns a multi-collection variable, but a multi-collection of string is not supported.
Valid data types: String

Note: This string will be treated as an expression and sent to P2 Server to be resolved. The function will return the result of the expression.

ParamsArray: The context parameters to be used to override the default context parameters of the expression.
Behaviour: Optional [0..n]
Dimension: Collection, SingleValue, Matrix
Valid data types: Null, Boolean, DateTime, Decimal, Integer, String, Duration, Error

Note: These parameters must be specified in pairs. The first parameter must be the name of a single value context parameter and the second parameter must be the value of it.
E.g. Eval("{myTag}", "sampleMethod", "Raw"). This will evaluate the "{myTag}" string with the sample method set to "Raw" in the string context. Note the use of {} around the tag name.

How to format data types

When to use

Both Eval() and EvalExp() can be used for evaluating expressions with overridden context parameters. The difference is that the first parameter of Eval() is a string while the first parameter of EvalExp() is an expression.

Here is an example showing the difference (without using context parameters, for simplicity):

Eval("1 + 2") -> returns 3
EvalExp(1 + 2) -> returns 3

The most obvious difference is that Eval() expects a string and therefore the first parameter must be wrapped in quotation marks. As EvalExp() expects an expression parameter, it doesn’t require quotes.

This means that you can use Eval() to evaluate a string which is generated and not hard-coded (as if it was an expression).

You should use Eval() if you have a string (which is often generated as the result of a sub-expression) and you want to treat that as an expression and evaluate it with or without overridden context parameters. Otherwise, use EvalExp().

Examples

Expression: Eval("1+2")
Expected result: 3

Expression: Eval("{" + entity + ":attribute}")
Expected result: Fetches the entity:attribute attribute value and return its value as a collection variable

Release History

  • Eval() 4.6.5
    • Changed String parameter to support an expression containing a MultiCollection value.
  • Eval() 4.0
    • Initial version

Comments are closed