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