Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a C# tuple into an F# tuple?

Tags:

f#

How do I convert a C# tuple into an F# tuple?

Specifically, I have a C# implementation of a Result type:

public class Result<T,E>
{
    public Result(T data) => Ok    = (true,data);

    public Result(E data) => Error = (true,data);

    public (bool,T) Ok    { get; }
    public (bool,E) Error { get; }
}

I want to take the tuple value of an Ok result or Error result and use it in my F# code.

Example:

let result = databaseService.getSomething(userIdValue) |> Async.AwaitTask |> Async.RunSynchronously

let isSuccessful,forms = result.Ok

However, I receive the following error:

Error FS0001 One tuple type is a struct tuple, the other is a reference tuple

In conclusion, I am confused on how to convert a C# tuple into an F# tuple. I found this link. But I wasn't able to leverage it for what I needed.

like image 673
Scott Nimrod Avatar asked Feb 07 '19 16:02

Scott Nimrod


1 Answers

C# 7.0 tuple syntax produces ValueTuple values which are different from F# tuples (the older Tuple classes). Luckilly since F# 4.1 you can use

let struct (isSuccessful, forms) = result.Ok

Notice the extra struct keyword in the pattern. You can use the same syntax to create new ValueTuple values, like let tup = struct (true, 42)

like image 135
Honza Brestan Avatar answered Oct 13 '22 11:10

Honza Brestan