Function calls
A function call computes a value from the values that you give to it, its arguments. EFX has three kinds of functions:
-
built-in functions, which come with the language;
-
user-defined functions, which you declare in template files;
-
dynamic functions, which you declare in rules files, and which receive their result from an external service during validation.
The names of user-defined and dynamic functions start with ?. This sets them apart from built-in functions.
Built-in function call
A built-in function call applies one of the functions of EFX to its arguments.
<function-name>() <function-name>(<argument>) <function-name>(<argument>, <argument>, …)
- <function-name>
-
the name of a built-in function, such as
count,string-lengthoradd-duration(see Built-in functions). - <argument>
-
an expression. Each argument gives the value of one parameter of the function.
Each built-in function has a fixed set of parameters, and some of them are optional. You give the arguments in the order of the parameters, separated by commas. You call a function without parameters, such as current-date, with empty parentheses.
Each parameter expects either a single value or a sequence, of a given type. An argument must give a value of that type. A parameter that expects a sequence also accepts a single value, as a sequence of one value. A parameter that expects a single value does not accept a sequence.
The translator checks each argument against its parameter (see
Type checking). Some functions accept several
types, as long as the types of their arguments match. For example, distinct-values accepts a sequence of any type
and gives a sequence of the same type.
The result has the type given in the entry of the function. You can use a function call wherever a value of that type is expected, also as an argument of another function call.
You can use some functions in template files only (see Template-only functions).
Restrictions
-
The number of arguments must be one that the function accepts:
substring(BT-00-Text)is an error. -
Each argument must have the type of its parameter.
string-length(BT-00-Number)is an error, and so isstring-length(BT-13-Text)fromND-Root, becauseBT-13-Textcan have several values there. -
You write function names in lower case:
COUNT(BT-13-Text)is an error. -
You call a built-in function without
?. EFX reads?count(BT-13-Text)as a call to a user-defined function namedcount.
Examples
count(BT-13-Text)
The number of values of BT-13-Text.
upper-case(substring(BT-00-Text, 1, 3))
The first three characters of BT-00-Text, in capital letters.
concat(BT-00-Text, ' ', text(BT-00-Number))
current-date()
EFX-2 adds many built-in functions and renames some of them. For example, add-measure is now add-duration (see
Built-in functions).
|
See also: User-defined function call
User-defined function call
A user-defined function call applies a function declared in the template file to its arguments.
?<function-name>() ?<function-name>(<argument>) ?<function-name>(<argument>, <argument>, …)
- <function-name>
-
the name of the function, as declared (see User-defined function declaration). You write it directly after the
?, without a space. - <argument>
-
an expression. Each argument gives the value of one parameter of the function.
You declare a user-defined function in a template file with LET, a return type, a name, typed parameters and an
expression. A call computes that expression, with each parameter set to the value of the matching argument.
You give the arguments in the order of the parameters. Each argument must have the type of its parameter. A parameter
declared with a sequence type, such as text*, accepts a sequence or a single value. A parameter declared
with a single-value type does not accept a sequence.
The result has the return type of the declaration: a single value, or a sequence when the declaration has a sequence type. So you can use a call wherever a value of that type is expected.
A function can call the functions declared before it.
Restrictions
-
You can declare and call user-defined functions in template files only. A rules file cannot declare them. A call in a single expression is an error, because nothing declares the function there.
-
You must declare a function before you use it: a call that comes before the declaration in the template file is an error. In particular, a function cannot call itself.
-
The number of arguments must equal the number of parameters. For a function with one parameter,
?double(1, 2)is an error. -
Each argument must have the type of its parameter. For a function with a number parameter,
?double('a')is an error. So is?double(BT-13-Number)fromND-Root, becauseBT-13-Numbercan have several values there.
Examples
The examples below are template files that declare a function and then call it.
LET number:?double(number:$n) = $n * 2;
WITH ND-Root DISPLAY ${?double(BT-00-Number) + 1};
The call ?double(BT-00-Number) gives a number, used here in an addition.
LET indicator:?is-large(number:$n) = $n > 1000;
WITH BT-00-Number[?is-large(BT-00-Number)] DISPLAY Large amount;
A call that gives an indicator, used in a predicate.
LET text*:?labels() = ['a', 'b'];
WITH ND-Root DISPLAY ${count(?labels())};
A function without parameters that gives a sequence.
view-templates/10.efx)LET indicator:?exists-common-value(text*:$seq1, text*:$seq2) = (count(value-intersect($seq1, $seq2)) > 0);
A function with two sequence parameters.
view-templates/10.efx)WITH BT-137-Lot[?exists-common-value((for text:$somerestricted in BT-14-Lot return $somerestricted), 'restricted-document')] DISPLAY #{field|name|BT-14-Lot};
A call of that function with a sequence and a single text as arguments.
| User-defined functions are new in EFX-2. |
See also: Dynamic function call
Dynamic function call
A dynamic function call asks an external service, during validation, whether its arguments meet a condition. You use it for conditions that the notice alone cannot show.
?<function-name>() ?<function-name>(<argument>) ?<function-name>(<argument>, <argument>, …)
- <function-name>
-
the name of a dynamic function declared in the rules file (see Dynamic function declaration).
- <argument>
-
an expression. Each argument gives the value of one parameter of the function.
You declare a dynamic function in a rules file with LET dynamic:, a name, typed parameters and the API endpoint to
call. You write a dynamic function call in the same way as a user-defined function call. When the validation checks
the rule, it sends the values of the arguments to the service. The call then gives an indicator: TRUE when the
service confirms the condition. The declaration states, with ON ERROR, what happens when the call fails.
You can write a dynamic function call in two places:
-
directly in the condition of a rule, that is, in an
ASSERT,REPORTorWHENclause (seeASSERTclause). There you can combine it with other conditions, for example withand,oror the functionnot; -
as the value of a dynamic variable,
dynamic:$<variable-name> = ?<function-name>(…). You can then use the variable in the conditions of rules (see Dynamic variable).
The translator checks the arguments as for a user-defined function: their number must equal the number of parameters, and each argument must have the type of its parameter.
Restrictions
-
You can declare and call dynamic functions in rules files only.
-
Outside a dynamic variable, a call can appear only in the condition of a rule. A call in the declaration of a variable of another type, such as
LET indicator:$k = ?is-known-buyer(BT-00-Identifier);, is an error. -
The value of a dynamic variable must be the call alone, not an expression that contains it.
-
The result is an indicator. A call used as a number, as in
1 + ?is-known-buyer(BT-00-Identifier), is an error.
Examples
ENDPOINT 'default' AT 'https://api.example.org/v1';
LET dynamic:?is-known-buyer(text:$id) CALL API;
---- STAGE 1a ----
WITH ND-Root
ASSERT ?is-known-buyer(BT-00-Identifier) AS ERROR R-AB1-C2D FOR BT-00-Identifier IN *;
A rules file that declares a dynamic function and calls it directly in a rule.
ASSERT BT-00-Text is present and not(?is-known-buyer(BT-00-Identifier))
A call combined with other conditions, in the same rules file.
WITH ND-Root, dynamic:$known = ?is-known-buyer(BT-00-Identifier)
ASSERT $known or BT-00-Text is present AS ERROR R-AB1-C2D FOR BT-00-Identifier IN *;
A dynamic variable, declared with the context of the rule-set, keeps the result of the call.
Dynamic functions are new in EFX-2. They replace the EFX-1 references to other notices, written notice(…), which no
longer exist.
|
See also: API endpoint declaration