Delegate part

Delegate parts provide models for functions.

Delegate parts have the following characteristics:
  • When you declare a variable, you specify a Delegate part name as a type. The variable is assigned the name of a matching function with the same signature as the Delegate part.
  • If you specify a delegate variable in the place of the function name on a function invocation, you can choose the function to be called dynamically.
  • Delegate parts are typically used as an infrastructure to register event handlers. For more information, see ExternalType for Java code
  • In situations that do not involve user interfaces, you can use Delegate parts as elements in a Dictionary of function pointers or as a way to dynamically invoke functions.
  • Delegate parts do not have properties.

Syntax


Syntax diagram for a Delegate part
delegatePartName
Specifies the name that you assign to the Delegate part.
parameters
A list of parameters that a matching function must have. The parameter types and modifiers that are defined for the delegate must be the same as those in the matching function. The parameter names might differ.
type
The type that a matching function must return.

Example

Here is an example of a Delegate part in the context of a program:
Delegate FunctionInvoker
  (displayStr STRING)
end

Program greetings
// declare delegate variable
invoker FunctionInvoker;

  Function main()
    // writeToScreen() must match Delegate parameters
    invoker = writeToScreen;

    invoker("Hello world");
  end

  Function writeToScreen(myMsg STRING)
    sysLib.WriteStdOut (myMsg);
  end
end

Compatibility

Table 1. Compatibility considerations for the Delegate part
Platform Issue
iSeries® The Delegate part is not available.