Aracılığıyla paylaş


Temsilciler (F#)

Bir temsilciyi bir nesne olarak bir işlev çağrısını temsil eder. F# işlevi değerleri normalde işlevleri olarak birinci değerleri temsil etmek için de kullanmalısınız; Ancak, delegates kullanılır.net Framework ve bu nedenle bunları beklediğiniz API ile birlikte olduğunda gereklidir. Geliştirme kitaplıkları diğer kullanılmak üzere tasarlanmış zaman da kullanılabilir.net Framework diller.

type delegate-typename = delegate of type1 -> type2

Notlar

Önceki sözdiziminde, type1 bağımsız değişken türü veya türlerini temsil eder ve type2 dönüş türü temsil eder. Tarafından temsil edilen bağımsız değişken türleri type1 otomatik olarak curried. Bu, bu tür hedef işlevinin bağımsız değişkenleri curried, kayıt düzeni formu kullanın ve kayıt düzeni formunda olan bağımsız değişkenler için parenthesized kayıt düzeni önerir. Otomatik currying hedef yöntemi ile eşleşen bir kayıt düzeni bağımsız bırakarak ayraç kümesi kaldırır. Kod örneği, her durumda kullanmalısınız sözdizimi için başvuruda bulunur.

Temsilciler, F# işlevi değerleri ve statik eklenebilecek veya yöntemleri örnek. F# işlevi değerleri Kurucular temsilci doğrudan bağımsız geçirilebilir. Bir static yöntem temsilcinin adını sınıfı ve yöntemi kullanarak oluşturun. Bir örnek yöntemi için nesne örneği ve bir bağımsız değişken yöntemi sağlar. Her iki durumda da üye erişimi operatörü (.) kullanılır.

Invoke Temsilci türü yöntemini çağıran kapsüllenmiş işlevi. Ayrıca, temsilciler işlevi değerleri olarak Invoke yöntemi adını parantezler olmadan başvurarak geçirilebilir.

Aşağıdaki kod, çeşitli yöntemler bir sınıf içinde temsil eden Temsilciler oluşturmak için sözdizimini göstermektedir. Yöntem bir static yöntem bir oluşum yöntemi olup, kayıt düzeni veya curried formundaki değişkenlerde olup bağlı, bildirmek ve temsilci atama için sözdizimi biraz farklıdır.

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 = new Delegate1( Test1.add )
let del2 : Delegate2 = new 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 = new Delegate1( testObject.Add )
let del4 : Delegate2 = new 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)

Aşağıdaki kod temsilcilerle çalışabilirsiniz farklı yollardan bazılarını gösterir.

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 = new 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

Önceki kod örneği çıktısı aşağıdaki gibidir.

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

Ayrıca bkz.

Kavramlar

Parametreleri ve bağımsız değişkenler (F#)

Diğer Kaynaklar

F# dil başvurusu

Olayları (F#)