65.9K
CodeProject is changing. Read more.
Home

Delegates and Callbacks in PPL

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jul 26, 2022

MIT

2 min read

viewsIcon

2800

PPL commands for using delegates and callbacks in ppl-mode and scr-mode, code examples with results

Introduction

I already wrote about (PPL). Since then, several libraries, commands, functions and samples were added. Delegates and callbacks are widely used in various languages, they were added to PPL also.

Format of Commands

PPL contains the following five commands to work with delegates and callbacks:

  • delegate - creation delegate
  • dlgtinstance - creation delegate instance
  • dlgtset - setting the function to delegate instance
  • dlgtcall - call function by delegate instance
  • callback - callback invokes synchronous callback method

delegate

delegate is created as an array whose elements define the method parameters.
Prefix "delegate_" is added to delegate name for internal using.

Format ppl: delegate | dlgt (delegate name) (param1) (param2)...
param:= var_name | var:var_name | array:array_name
Format scr: delegate | dlgt delegate name (param1, param2,…)

dlgtinstance

dlgtinstance is created as an array with two elements, first is delegate name, second is empty and will be set by dlgtset.
Delegate parameters types must be matched types of function parameters. Prefix "dlgtinstance_" is added to delegate instance name for internal using.

Format ppl: dlgtinstance (delegate instance_name)(delegate_name)
Format scr: dlgtinstance delegate instance_name delegate_name

dlgtset

dlgtset sets function name as second element in dlgtinstance array.

Format ppl: dlgtset (delegate instance_name)(function_name)
Format scr: dlgtset delegate instance_name function_name

dlgtcall

dlgtcall calls function defined in dlgtset.

Format ppl: dlgtcall(delegate instance_name)(arg1)(arg2)(arg3)…
Format scr: dlgtcall delegate instance_name(arg1,arg2,arg3,…)

callback

callback invokes synchronous callback method.

Format ppl: callback (callback_name)(arg1)(arg2)(arg3)…
Format scr: callback callback_name (arg1,arg2,arg3,…)

Samples of Code

The following C# sample - creation delegate, instance and call function:

        public static void DelegateMethod(string message)
        {  Console.WriteLine(message);   }
        delegate void Del(string message);   // create delegate
        Del handler;                         // Instantiate the delegate.
        handler = DelegateMethod;            // Set the delegate
        handler("Hello World");              // Call the delegate.      

The same PPL code in scr-mode looks like this:

      >delegate Del(var:message);
      >display;
        
      -N2     NS
      ---N3   Global
      -----L0 empty   (const)
      -----N4 delegate_Del    [Array 1]
      -------L0       #       [var:message]
        
     >dlgtinstance handler Del;
     >display;
       
     -N2     NS
     ---N3   Global
     -----L0 empty   (const)
     -----N4 delegate_Del    [Array 1]
     -------L0       #       [var:message]
     -----N4 dlgtinstance_handler     [Array 2]
     -------L0       #       ["delegate_Del"]
      
     >function DelegateMethod(var:message) {write#("{0}",message)};
     >display Functions.DelegateMethod
      
     -N3     DelegateMethod  [function]
     ---L0   var:message
     ---N4   #               [internal_block]
     -----N5 write
     -------L0       "{0}"
     -------L1       message
      
    >dlgtset handler DelegateMethod;
    >display;
     
    -N2     NS
    ---N3   Global
    -----L0 empty   (const)
    -----N4 delegate_Del    [Array 1]
    -------L0       #       [var:message]
    -----N4 dlgtinstance_handler     [Array 2]
    -------L0       #       ["delegate_Del"]
    -------L1       #       [DelegateMethod]
     
    >dlgtcall handler (""Hello world);
    
    Hello world

Command display in the above presents contents of ppl-arrays, created by executed commands.

Sample PPL in scr-mode with callback:

  function cb1(var:n){write#("function cb1 {0}",n)};
  function cb2(var:n) { write#("function cb2 {0}",n);}

  function f(array:x,var:str) 
  { 
    callback x(str); 
  }

  delegate dd (var:n);
  dlgtinstance instance dd;
  dlgtset instance cb1;
  call f(instance,"PPL");
  dlgtset instance cb2;
  call f(instance,"PPL");
  
  result:
  function cb1 PPL
  function cb2 PPL 

Sample of using callback in function ArrayForEach (see Functions\CommonFunctions.ppl in CPPL.zip).

  ArrayForEach(array:arr, array:callback_name) –  
               calls a callback function once foreach array element
  function sumfunc(array:arr,var:i)
  {
    set result = result + arr[i];
  }

  var result = 0;                           
  delegate dd (array:arr,var i);        
  dlgtinstance instance dd;      
  dlgtset instance sumfunc;
  call ArrayForEach({1,2,3,4,5},instance);  
  write#("result = {0}",result);    
  
  result = 15    

Conclusion

Using delegates and callbacks makes it easy to write code and I hope PPL programmers will use them in writing scripts.

First Article in the Series

History

  • 25th July, 2022: Initial version