Skip to content

Computing Types

anymaker edited this page Apr 24, 2021 · 2 revisions

What is Type

Type conversion

Every time when computer calculated an expression, it converts the right value to the type of the left value.
You usually don't need to worry about this. Conversion is performed automatically according to standard rules.
As example String 'Yes' can be converted to Boolean true.
For work on converting values - responsible are Types.

You can also use a type converter without formulas.

ObjCalcEngine engine = new ObjCalcEngine();
long value = engine.toType(long.class, object);

object - is any value that can be converted to type long

Example

long value = engine.toType(long.class, new Date());

When you do a formula calculation, you can pass in the type of the final return value. This conversion is done by types.

Example

Boolean v1 = engine.calc("true", Boolean.class);
Boolean v2 = engine.calc(".field in ('AAA','BBB')", myObj, Boolean.class);

Calculation of operations

The second responsibility of Types is to compute operations.
Each type computes operations in its own way. As an example, when calculating the operation + - integers increase their value 1 + 2 = 3, and strings concatenated the values of both operands '1' + '2' = '12'.

In the calculation, the value of the second operand is converted to either the type of the first operand or the expected type of the second operand for this operation.
As an example, in an in operation, the second operand is expected to be a list. In this case, the value of the third operand is converted to a list, even if there is a single value.