The design suggestion Stronger type directed conversion from functions to .Net delegates has been marked "approved in principle". This RFC covers the detailed proposal for this suggestion.
- Approved in principle
- Suggestion
- Details: under discussion
- Implementation: not started
Currently type directed conversion from functions to .NET delegates only works if called method have overloads. It will make type directed conversion more consistent and predictable if it behaves the same regardless whether method have overloads or not.
It is very confusing to see a compilation error when type directed conversion didn't worked because method lacked unrelated overload, e.g. the following would fail:
type Test() =
member this.Test(action : System.Func<int, int>) = action.Invoke(1)
let test x = x + 1
Test().Test (test) // This expression was expected to have type System.Func<int,int> but here has type int -> int
But adding unrelated overload to that method enables type directed conversion hence making code compileable:
type Test() =
member this.Test(x) = x + 1 |> ignore
member this.Test(action : System.Func<int, int>) = action.Invoke(1)
let test x = x + 1
Test().Test (test)
Type directed conversion extends to happen even if a method does not have overloads. From the user perspective nothing changes except that functions can be passed as delegates in more scenarios.
TBD
As a workaround, wrapping function into a lambda works in all cases, e.g.
let test x = x + 1
Test().Test (fun x -> test x)
TBD