In F# how can I produce an expression with a type of Func?

前端 未结 3 664
孤街浪徒
孤街浪徒 2021-02-12 22:38

I\'m working with an api that requires a value of type Func. (Specifically, I\'m trying to call ModelMetadataProviders.Current.GetMetadataForType().

How can I construct

3条回答
  •  甜味超标
    2021-02-12 22:53

    When calling a method that takes any delegate of the Func you shouldn't need to explicitly create the delegate, because F# implicitly converts lambda expressions to delegate type (in member calls). I think that just calling the method with lambda function should work (if it doesn't, could you share the error message?)

    Here is a simple example that demonstrates this:

    type Foo() = 
      member x.Bar(a:System.Func) = a.Invoke()
    
    let f = Foo()
    let rnd = f.Bar(fun () -> new Random() :> obj)
    

    In your case, I suppose something like this should work:

    m.GetMetadataForType((fun () ->  :> obj), modelType)
    

    Note that you need explicit upcast (expr :> obj), to make sure the lambda function returns the right type (obj). If you want to assign the lambda function to a local value using let, then it won't work, because implicit conversion works only when it is passed as an argument directly. However, in that case, it makes the code a bit nicer.

提交回复
热议问题