Question

I am writing an F# interop class to be used from C#. I thought that F# had an implicit conversion from .NET Tuple<> type (similar to IEnumerable treated as seq), so I wrote the following code:

type MyClass() = 
    member this.MyMethod (t: Tuple<int, int>) =
        let meaningOfLife (t : int * int) = 42
        meaningOfLife t

This code fails to compile with the following error: error FS0001: This expression was expected to have type int * int but here has type Tuple

Then how to convert tuples between C# and F# (and back)?

Était-ce utile?

La solution

If you're targeting .NET 4.0 and above, you don't need to specify the object Tuple. F# tuples get automatically compiled to Tuple. You can use:

type MyClass() = 
  member this.MyMethod (t: int * int) =
    let meaningOfLife (t : int * int) = 42
    meaningOfLife t

And it should work fine from C#.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top