Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 2.07 KB

FS-1035-consistent-delegate-type-directed-conversion.md

File metadata and controls

66 lines (48 loc) · 2.07 KB

F# RFC FS-1035 - Consistent type directed conversion from functions to .NET delegates

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.

Summary

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.

Motivation

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)

Detailed design

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.

Drawbacks

TBD

Alternatives

Don't do it

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)

Unresolved questions

TBD