Click here to Skip to main content
15,891,688 members
Articles / Programming Languages / C#
Tip/Trick

Delegates and Callbacks in PPL

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Jul 2022MIT2 min read 2.7K   1  
PPL commands for using delegates and callbacks in ppl-mode and scr-mode, code examples with results
Internals, formats, samples using delegates and callbacks in PPL

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:

C#
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:

PPL
  >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:

PPL
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).

PPL
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

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior)
Slovenia Slovenia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --