委派 (F#)

委派會將函式呼叫表示為物件。 在 F# 中,通常應該使用函式值以將函式表示為第一類別值;不過因為委派會在.NET Framework 中使用,因此當您與預期使用委派的 API 交互操作時,就會需要委派。 製作專門設計為可從其他 .NET Framework 語言使用的程式庫時,也可以使用委派。

語法

type delegate-typename = delegate of type1 -> type2

備註

在上一個語法中,type1 表示引數型別,而 type2 表示傳回型別。 type1 代表的引數型別會自動進行局部調用。 這表示,如果目標函式的引數經過局部調用,則您會針對此型別使用元組形式,而針對已是元組形式的引數,則使用以括弧括住的元組。 自動局部調用會移除一組括弧,並保留符合目標方法的元組引數。 請參閱程式碼範例,以了解您應該在每個案例中使用的語法。

委派可以附加至 F# 函式值、靜態或執行個體方法。 F# 函式值可以直接當做引數傳遞至委派建構函式。 針對靜態方法,您可以使用類別和方法的名稱來建構委派。 針對執行個體方法,您可以在一個引數中提供物件執行個體和方法。 在這兩種情況下,都會使用成員存取運算子 (.)。

委派型別上的 Invoke 方法會呼叫封裝的函式。 此外,藉由參考叫用方法名稱 (不加上括弧),即可將委派當做函式值傳遞。

下列程式碼顯示的語法,可用來建立代表類別中各種方法的委派。 根據方法是靜態方法還是執行個體方法,以及是否有元組形式或局部調用形式的引數,宣告和指派委派的語法會稍有不同。

type Test1() =
  static member add(a : int, b : int) =
     a + b
  static member add2 (a : int) (b : int) =
     a + b

  member x.Add(a : int, b : int) =
     a + b
  member x.Add2 (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(Test1.add)
let del2 = Delegate2(Test1.add2)

let testObject = Test1()

// For instance methods, use the instance value name, the dot operator, and the instance method name.
let del3 = Delegate1(testObject.Add)
let del4 = Delegate2(testObject.Add2)

for (a, b) in [ (100, 200); (10, 20) ] 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)

下列程式碼顯示一些可以使用委派的不同方式。

type Delegate1 = delegate of int * char -> string

let replicate n c = String.replicate n (string c)

// An F# function value constructed from an unapplied let-bound function
let function1 = replicate

// A delegate object constructed from an F# function value
let delObject = Delegate1(function1)

// An F# function value constructed from an unapplied .NET member
let functionValue = delObject.Invoke

List.map (fun c -> functionValue(5,c)) ['a'; 'b'; 'c']
|> List.iter (printfn "%s")

// Or if you want to get back the same curried signature
let replicate' n c =  delObject.Invoke(n,c)

// You can pass a lambda expression as an argument to a function expecting a compatible delegate type
// System.Array.ConvertAll takes an array and a converter delegate that transforms an element from
// one type to another according to a specified function.
let stringArray = System.Array.ConvertAll([|'a';'b'|], fun c -> replicate' 3 c)
printfn "%A" stringArray

上述程式碼範例的輸出如下。

aaaaa
bbbbb
ccccc
[|"aaa"; "bbb"|]

可以將名稱新增至委派參數,如下所示:

// http://www.pinvoke.net/default.aspx/user32/WinEventDelegate.html
type WinEventDelegate = delegate of hWinEventHook:nativeint * eventType:uint32 * hWnd:nativeint * idObject:int * idChild:int * dwEventThread:uint32 * dwmsEventTime:uint32 -> unit

委派參數名稱是選擇性的,且會顯示在 Invoke 方法中。 它們不需要與實作中的參數名稱相符。 它們僅適用於局部調用形式,而非元組形式。

type D1 = delegate of item1: int * item2: string -> unit
let a = D1(fun a b -> printf "%s" b)
a.Invoke(item2 = "a", item1 = 1) // Calling with named arguments

type D2 = delegate of int * item2: string -> unit // Omitting one name
let b = D2(fun a b -> printf "%s" b)
b.Invoke(1, item2 = "a")

上述程式碼範例的輸出如下。

aa

另請參閱