Typing
EFX checks the types of every expression when it translates it, before any notice is read. So you find a type error at once, with its position in the file, and not as a wrong result when a notice is processed. You do not write types in expressions, except in declarations: the translator finds them itself. This page explains how the translator finds the types, which rules it checks, and how you convert a value from one data type to another.
Type checking
Type checking makes certain that every operator, function and clause receives the kind of values that it accepts.
For every part of an expression, the translator knows two things: its data type, such as text or number, and whether it gives a single value (scalar) or a sequence (see Data types). It finds them as follows:
-
A literal has the type that its form shows:
'Lot 1'is a text,42a number,2024-03-01Za date (see Literals). -
A field reference has the data type of the field in the SDK (see Field types and their EFX data types). Whether it gives a single value or a sequence depends on the repeatability of the field from the context (see Repeatability).
-
An attribute reference and a raw value are always texts.
-
A variable, a parameter or a function declared in the file has the type written in its declaration, such as
numberortext*. A dynamic function always gives an indicator. -
A context variable that holds a field has the data type of the field. A context variable that holds a node has no value, and neither has a node reference.
-
A dictionary lookup gives a sequence of the type of the field that the dictionary indexes.
-
An operator, a built-in function, a conditional expression or an iteration gives the type described in its entry. For example, the difference between two dates is a duration,
countgives a number, and an iteration gives a sequence.
With these types, the translator checks the following rules:
-
Each operator, function and clause accepts only the data types given in its entry. The two sides of a comparison must have the same data type:
BT-00-Text == BT-00-Numberis an error, and so isBT-00-Duration + BT-00-Text. -
EFX never converts a value to another data type by itself. To convert a value, you call a conversion function (see Type conversion).
-
You can combine fields of different SDK types that have the same data type: a multilingual text, a code and a URL are all texts.
-
Where a sequence is expected, a single value is accepted, as a sequence of one value.
-
Where a single value is required, a sequence is never accepted, even when it contains only one value. From
ND-Root, for example,BT-13-Number > 0is an error, becauseBT-13-Numbercan give several values there. In that case, select one value, for example with an index, or write the expression in a context from which the field gives a single value. -
A node has no value, so
ND-SubNode == 'x'is an error. You can use a node only where a place in the notice is expected: as a context, in a context variable, after::, withis present, and incount.
Some operators and functions accept any data type, as long as the types of their operands match. For example, in
accepts a value of any type with a sequence of the same type, and distinct-values accepts a sequence of any type and
gives a sequence of the same type.
When one of these rules is broken, the translator reports an error. The message gives the line and the column, says which construct does not accept which types and, where possible, how to correct the expression.
Examples
WITH ND-Root COMPUTE BT-00-Number + 1 > 10
BT-00-Number is a number, because of its type in the SDK. The sum is a number, and the comparison gives an
indicator.
WITH ND-Root COMPUTE text(BT-00-Number) == BT-00-Text
The number is converted to a text before the comparison.
WITH ND-Root COMPUTE BT-00-Text-Multilingual[at 1] == BT-00-Text
A multilingual field gives one text for each language, so a sequence. The index selects one text.
WITH ND-Root COMPUTE 'x' in [BT-00-Text, BT-00-Code]
in accepts a text and a sequence of texts. A text field and a code field both have text values.
LET number:$limit = 100;
LET indicator:?is-large(number:$amount) = $amount > $limit;
DISPLAY ${?is-large(BT-00-Number)};
In this template file, $limit, $amount and ?is-large have the types written in their declarations.
|
In EFX-1, you could state the type of a reference by writing the type name and a colon before it, for example
Two more things have changed. First, EFX-2 accepts a single value (scalar) wherever it expects a sequence. Second, it no longer accepts a reference that may give several values where only one value is needed. EFX-1 could not tell whether a field reference gave one value or several, so it could not report this mistake: a comparison with a repeatable field, for example, was true as soon as any one of its values matched. EFX-2 reports such an error when the expression is translated (see Comparison operators). |
Type conversion
A type conversion changes a value of one data type into a value of another. Because EFX never converts a value by itself, you always write a conversion as a call of a conversion function, named after the type that it gives.
text(<value>) number(<value>) indicator(<number>) date(<text>) time(<text>) day-time-duration(<text>) year-month-duration(<text>)
- <value>
-
for
text, a number, an indicator, a date, a time or a duration; fornumber, a text or an indicator. - <number>
-
a number.
- <text>
-
a text in the standard form of the type that you want, such as
'2024-03-01Z'for a date or'P3D'for a duration.
The table shows all the conversions that exist. Any other conversion, such as from a text to an indicator
(indicator('true')) or from a date to a number, is an error.
| To | From | Function |
|---|---|---|
text |
number, indicator, date, time, duration |
text (also spelled |
number |
text, indicator |
|
indicator |
number |
|
date |
text |
|
time |
text |
|
duration |
text |
Other functions and operators also give a value of another type, but they are not conversions. For example,
string-length gives the number of characters of a text, year gives the year of a date as a number, and the
difference between two dates is a duration. In templates, the formatting functions, such as format-number and
format-medium, turn numbers, dates and times into texts for display. The appendix
Type conversions lists all of them.
Examples
WITH ND-Root COMPUTE number(BT-00-Text) > 5
WITH ND-Root COMPUTE date(BT-00-Text) < BT-00-StartDate
WITH ND-Root COMPUTE number(BT-00-Indicator) + 1
An indicator converted to a number: 1 for true, 0 for false.
EFX-1 had fewer conversions: string from a number, number from a text, and date, time, day-time-duration and
year-month-duration from a text. EFX-2 adds the name text for string, conversions to text from all the other
data types, and conversions in both directions between indicators and numbers.
|
See also: Conversion functions