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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With