Type conversion
EPL requires strict type conformance in expressions, assignment and other statements, parameters in action and method calls, and most other constructs. The only implicit conversion in EPL is to convert concrete types to the any type. This means that:
The left and right operands of most binary operators must be of the same type.
An actual parameter passed in a method or action invocation must be of the same type as the type of the corresponding formal parameter in the action or method definition, or the parameter's type must be the
any type.
The expression on the right side of an assignment statement must be the same type as that of the target variable, or the target variable is of the
any type, or an
optional of the value type.
The expression in a variable initializer must be the same type as that of the target variable, or the target variable is of the
any type, or an
optional of the value type.
The expression in a subscript expression (to locate a sequence entry) must be integer. The expression used as an index for a dictionary must be the same type as the dictionary's key type (or the dictionary's key type is
any).
The expression in a
return statement must be the same type as that of the action's
returns clause, or the return type is the
any type.
For conversions between concrete types, the inbuilt methods on each type include a set of methods which perform type conversion. For example:
string number;
integer value;
number := "10";
value := number.toInteger();
This illustrates how to map a string to an integer. The string must start with some numeric characters, and only these are considered. So if the string's value was 10h, the integer value obtained from it would have been 10. Had the conversion not been possible because the string did not start with a valid numeric value, then value would have been set to 0.
These method calls can also be made inside event expressions as long as the type of the value returned is of the same type as the parameter where it is used. Therefore one can write:
on all StockTick("ACME", number.toFloat());
Method calls can be chained. For example one can write:
print ((2 + 3).toString().toFloat() + 4.0).toString();
Note that as shown in this example, method calls can also be made on literals.
The following table indicates the source and target type-pairs for which type conversion methods are provided.
In the table above,
assign means values of the type can be directly assigned to another variable of the same type, without calling a type conversion method.
clone means a value of the type can be copied by calling the
clone() method.
cast means that
any type values can be cast (see
Handling the any type for more information).