Function references
Function can be referenced and invoked dynamically in a CALL
instruction, or in an expression.
Syntax
FUNCTION (
parameter-name type-specification [
attributes-list ]
[
,...]
)
[
attributes-list ]
[
RETURNS {
type-specification [
attributes-list ]
|
( type-specification [
attributes-list ]
[
,...]
)
|
( )
}
]
- parameter-name is the name of a formal argument of the function type.
- type-specification can be one of:
- A primitive type
- A record definition
- An array definition
- A dictionary definition
- A function type definition
- The name of a user defined type
- The name of a built-in class
- The name of an imported extension class
- The name of an imported Java class
- attributes-list is a comma-separated list of name = value pairs or name attributes, and defines attributes for the function.
Purpose of function references
A function reference points to a function definition, that can be called at runtime. The actual function is not known at compile time, only the function type (number and type of parameters and return values) is known.
This feature allows you to manipulate functions dynamically, for example to implement generic module, that can be configured with callback functions.
Function references are based on function types. Referenced functions must be defined with the
syntax defining parameter types in parentheses (and the RETURNS
clause, if the
functions return values). For more details, see FUNCTION
syntax 2.
Defining function types
A function type identifies the signature of a function from the number, names and types of parameters and return values of that function:
FUNCTION(p1 INT, p2 INT) RETURNS INT
The name of the parameters is part of the function signature. Function types using the same number of parameters/types and return types, but different parameter names are considered as a different function types by the compiler.
DEFINE fx FUNCTION(p1 INT, p2 INT) RETURNS INT
To simplify function reference usage, define a user-type with the TYPE
instruction, with the function type that will match functions to be called by reference:
TYPE callback_function FUNCTION(p1 INT, p2 INT) RETURNS INT
For more details about user-defined type definitions, see Types.
Variable definition for function references
When the user-type for the function reference is available, declare a program variable to hold such function reference:
DEFINE callback callback_function
For more details about variable definitions, see Variables.
Get the FUNCTION reference
To get the reference of a function, use the FUNCTION
keyword followed by the
name of the function to be referenced. The function must be defined in the current module, or in a
module imported with IMPORT
FGL
.
LET callback = FUNCTION add
In the above example, the function "add()
" must be defined with the same
function type as the "callback
" variable.
For more details, see FUNCTION func-spec.
Invoking a function with the CALL statement
CALL
instruction, by using the variable.
The referenced function will be called as in a regular function
call:CALL callback(100,200) RETURNING result
Using function references in expressions
LET get_count_func = FUNCTION get_total_items()
LET c = get_count_func()
LET get_count_func = FUNCTION get_total_elements()
LET c = c + get_count_func()
Passing function references as function parameters
CALL process( FUNCTION add, FUNCTION sub, callback )
...
FUNCTION process( f1 callback_function,
f2 callback_function,
f3 callback_function )
DISPLAY f1(100,200) + f2(200,50) + f3(150,300)
END FUNCTION