F# Tutorial on F# Delegates

a delegate is a reference type variable that holds the reference to a method. the reference can be changed at runtime. f# delegates are similar to pointers to functions, in c or c++.

declaring delegates

delegate declaration determines the methods that can be referenced by the delegate. a delegate can refer to a method, which have the same signature as that of the delegate.

syntax for delegate declaration is −

type delegate-typename = delegate of type1 -> type2

for example, consider the delegates −

// delegate1 works with tuple arguments.
type delegate1 = delegate of (int * int) -> int

// delegate2 works with curried arguments.
type delegate2 = delegate of int * int -> int

both the delegates can be used to reference any method that has two int parameters and returns an int type variable.

in the syntax −

  • type1 represents the argument type(s).

  • type2 represents the return type.

please note −

  • the argument types are automatically curried.

  • delegates can be attached to function values, and static or instance methods.

  • f# function values can be passed directly as arguments to delegate constructors.

  • for a static method the delegate is called by using the name of the class and the method. for an instance method, the name of the object instance and method is used.

  • the invoke method on the delegate type calls the encapsulated function.

  • also, delegates can be passed as function values by referencing the invoke method name without the parentheses.

the following example demonstrates the concept −

example

type myclass() =
   static member add(a : int, b : int) =
      a + b
   static member sub (a : int) (b : int) =
      a - b
   member x.add(a : int, b : int) =
      a + b
   member x.sub(a : int) (b : int) =
      a - b

// delegate1 works with tuple arguments.
type delegate1 = delegate of (int * int) -> int

// delegate2 works with curried arguments.
type delegate2 = delegate of int * int -> int

let invokedelegate1 (dlg : delegate1) (a : int) (b: int) =
   dlg.invoke(a, b)
let invokedelegate2 (dlg : delegate2) (a : int) (b: int) =
   dlg.invoke(a, b)

// for static methods, use the class name, the dot operator, and the
// name of the static method.
let del1 : delegate1 = new delegate1( myclass.add )
let del2 : delegate2 = new delegate2( myclass.sub )
let mc = myclass()

// for instance methods, use the instance value name, the dot operator, 
// and the instance method name.

let del3 : delegate1 = new delegate1( mc.add )
let del4 : delegate2 = new delegate2( mc.sub )

for (a, b) in [ (400, 200); (100, 45) ] do
   printfn "%d + %d = %d" a b (invokedelegate1 del1 a b)
   printfn "%d - %d = %d" a b (invokedelegate2 del2 a b)
   printfn "%d + %d = %d" a b (invokedelegate1 del3 a b)
   printfn "%d - %d = %d" a b (invokedelegate2 del4 a b)

when you compile and execute the program, it yields the following output −

400 + 200 = 600
400 - 200 = 200
400 + 200 = 600
400 - 200 = 200
100 + 45 = 145
100 - 45 = 55
100 + 45 = 145
100 - 45 = 55