在F#中, 委托是引用类型。它使我们可以将函数作为对象来调用。这是该语言的功能。与其他功能编程语言相比, 它具有优势。
F#语言中的Delegate的语法如下:
type delegate-typename = delegate of type1 ->
type2
F#委托示例
type Deligate() =static member mul(a : int, b : int) =a * bmember x.Mul(a : int, b : int) =a * btype Multiply = delegate of (int * int) ->
intlet getIt (d : Multiply) (a : int) (b: int) =d.Invoke(a, b)let d : Multiply = new Multiply( Deligate.mul )for (a, b) in [(5, 8) ] doprintfn "%d * %d = %d" a b (getIt d a b)
【F#代理或委托】输出:
5 * 8 = 40