First Class Functions in Genero BDL (4gl)

A programming language supports first class functions, when function references can be assigned as arguments and returned as values from other functions.

In a language such as Genero BDL that doesn’t natively support the definition of classes, first class functions are helpful for code injections and callback functionality.

For instance, if a module performs a lengthy task, a callback can provide feedback to the calling module displaying progress to the user. This can be done with function references.

Starting with version 3.10 of Genero BDL, first class functions were added to the language through the addition of a function type definition and function type parameters.

For example if I can can declare a function reference type that can then be used in variable, and parameter definition.


TYPE callback_function_type FUNCTION(percent int, message string) returns int
FUNCTION do_work(progress_callback callback_function_type)
define
 rsp int 
...
...
call progress_callback(25,"Making progress")
 returning rsp
...
end function

Using types is my preferred way to define first class function types but the FUNCTION(…) syntax can be used to define first class functions as local, module and function arguments.

DEFINE callback FUNCTION(percent int, message string) returns int

Similar syntax can be used to define a function argument to another function

FUNCTION do_work(progress_callback FUNCTION(percent INT, message INT) returns int)

I find that syntax can be awkward so I prefer to use types to declare first class function parameters as shown in the first two examples.

FUNCTION display_progress(percent int, message string) returns int
...
end function

For myself, first class functions are useful for callback and injecting additional functionality into modules.

The documentation is available at: https://4js.com/online_documentation/fjs-fgl-3.10.00-manual-html/#fgl-topics/c_fgl_Functions_references.html