Chain asynchronous operations that return either using the Ext language in C #

I am using the Language-Ext library for C # and I am trying to link asynchronous operations that return a type Either. Let's say that I have three functions that return an integer if they succeed, and a string if it fails, and another function that summarizes the result of the previous three functions. In the implementation examples below Op3, it crashes 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 link these operations, and I am trying to do this as follows:

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 do not compile the code, because I get an error cannot convert from 'LanguageExt.Either<string, int>' to 'int'for the arguments Calculate. How do I link these functions?

+4
source share
1

x, y z int, Either<string, int> Calculate(int x, int y, int z), : Calculate(Either<string, int> x, Either<string, int> y, Either<string, int>z)

x.{The int getter property}
0

Source: https://habr.com/ru/post/1693830/


All Articles