{"id":143,"date":"2019-10-01T00:27:38","date_gmt":"2019-10-01T00:27:38","guid":{"rendered":"https:\/\/binaryopus.com\/?p=143"},"modified":"2019-10-01T00:59:36","modified_gmt":"2019-10-01T00:59:36","slug":"first-class-functions-in-genero-bdl-4gl","status":"publish","type":"post","link":"https:\/\/binaryopus.com\/?p=143","title":{"rendered":"First Class Functions in Genero BDL (4gl)"},"content":{"rendered":"\n<p>A programming language supports first class functions, when function references can be assigned as arguments and returned as values from other functions. <\/p>\n\n\n\n<p>In a language such as Genero BDL that doesn&#8217;t natively support the definition of classes, first class functions are helpful for code injections and callback functionality.  <\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>For example if I can can declare a function reference type that can then be used in variable, and parameter definition.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nTYPE callback_function_type FUNCTION(percent int, message string) returns int\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>FUNCTION do_work(progress_callback callback_function_type)\ndefine\n rsp int \n...\n...\ncall progress_callback(25,\"Making progress\")\n returning rsp\n...\nend function<\/code><\/pre>\n\n\n\n<p>Using types is my preferred way to define first class function types but the FUNCTION(&#8230;) syntax can be used to define first class functions as local, module and function arguments. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DEFINE callback FUNCTION(percent int, message string) returns int<\/code><\/pre>\n\n\n\n<p>Similar syntax can be used to define a function argument to another function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FUNCTION do_work(progress_callback FUNCTION(percent INT, message INT) returns int)<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FUNCTION display_progress(percent int, message string) returns int\n...\nend function<\/code><\/pre>\n\n\n\n<p>For myself, first class functions are useful for callback and injecting additional functionality into modules. <\/p>\n\n\n\n<p>The documentation is available at: <a href=\"https:\/\/4js.com\/online_documentation\/fjs-fgl-3.10.00-manual-html\/#fgl-topics\/c_fgl_Functions_references.html\">https:\/\/4js.com\/online_documentation\/fjs-fgl-3.10.00-manual-html\/#fgl-topics\/c_fgl_Functions_references.html<\/a><\/p>\n\n\n\n\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,8],"tags":[],"_links":{"self":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/143"}],"collection":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=143"}],"version-history":[{"count":4,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/binaryopus.com\/index.php?rest_route=\/wp\/v2\/posts\/143\/revisions\/147"}],"wp:attachment":[{"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryopus.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}