I am using the Language-Ext library for C# and I am trying to chain asynchronous operations that return an Either
type. Let's say that I have three functions that would return an integer if they succeed and a string if the fail, and another function that sums the result of the previous three functions. In the sample implementations below Op3
fails and returns a string.
public static async Task<Either<string, int>> Op1()
{
return await Task.FromResult(1);
}
public static async Task<Either<string, int>> Op2()
{
return await Task.FromResult(2);
}
public static async Task<Either<string, int>> Op3()
{
return await Task.FromResult("error");
}
public static async Task<Either<string, int>> Calculate(int x, int y, int z)
{
return await Task.FromResult(x + y + z);
}
I want to chain these operations and I am trying to do it like this:
var res = await (from x in Op1()
from y in Op2()
from z in Op3()
from w in Calculate(x, y, z)
select w);
But we the code does not compile because I get the error cannot convert from 'LanguageExt.Either<string, int>' to 'int'
for the arguments of Calculate
. How should I chain these functions?