Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert between F# and C# tuples?

Tags:

f#

c#-to-f#

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)?

like image 408
Vagif Abilov Avatar asked Jul 21 '13 18:07

Vagif Abilov


People also ask

How do you convert C to F easily?

To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.

How do you convert F to C scale?

The Fahrenheit to Celsius formula represents the conversion of degree Fahrenheit to degree Celsius. The formula for Fahrenheit to Celsius is °C = [(°F-32)×5]/9.

IS F to C exact?

Key Takeaways: Fahrenheit to Celsius The formula for converting Fahrenheit to Celsius is C = 5/9(F-32). Fahrenheit and Celsius are the same at -40°. At ordinary temperatures, Fahrenheit is a larger number than Celsius. For example, body temperature is 98.6 °F or 37 °C.


1 Answers

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#.

like image 174
sker Avatar answered Nov 11 '22 07:11

sker