return 在 C# 中使用 Language-Ext 的链式异步操作
Chain asynchronous operations that return Either using Language-Ext in C#
我正在使用 C# 的 Language-Ext 库,我正在尝试链接 return 和 Either
类型的异步操作。假设我有三个函数,如果成功则 return 一个整数,如果失败则一个字符串,另一个函数将前三个函数的结果相加。在下面的示例实现中 Op3
失败并且 return 是一个字符串。
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);
}
我想链接这些操作,我正在尝试这样做:
var res = await (from x in Op1()
from y in Op2()
from z in Op3()
from w in Calculate(x, y, z)
select w);
但我们的代码无法编译,因为我得到 Calculate
参数的错误 cannot convert from 'LanguageExt.Either<string, int>' to 'int'
。我应该如何链接这些函数?
x、y 和 z 不是 int 类型,而是 Either<string, int>
更改 Calculate(int x, int y, int z)
以接受 Either 调用的实例:Calculate(Either<string, int> x, Either<string, int> y, Either<string, int>z)
,或传递
x.{The int getter property}
问题是 LINQ 查询无法计算出要使用哪个版本的 SelectMany
,因为第二行中没有使用 x
。您可以通过将 Task<Either<L, R>>
转换为 EitherAsync<L, R>
:
来解决这个问题
public static async Task<int> M()
{
var res = from x in Op1().ToAsync()
from y in Op2().ToAsync()
from z in Op3().ToAsync()
from w in Calculate(x, y, z).ToAsync()
select w;
return await res.IfLeft(0);
}
或者,而不是 returning Task<Either<L, R>>
return EitherAsync<L, R>
:
public static EitherAsync<string, int> Op1() =>
1;
public static EitherAsync<string, int> Op2() =>
2;
public static EitherAsync<string, int> Op3() =>
3;
public static EitherAsync<string, int> Calculate(int x, int y, int z) =>
x + y + z;
public static async Task<int> M()
{
var res = from x in Op1()
from y in Op2()
from z in Op3()
from w in Calculate(x, y, z)
select w;
return await res.IfLeft(0);
}
我正在使用 C# 的 Language-Ext 库,我正在尝试链接 return 和 Either
类型的异步操作。假设我有三个函数,如果成功则 return 一个整数,如果失败则一个字符串,另一个函数将前三个函数的结果相加。在下面的示例实现中 Op3
失败并且 return 是一个字符串。
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);
}
我想链接这些操作,我正在尝试这样做:
var res = await (from x in Op1()
from y in Op2()
from z in Op3()
from w in Calculate(x, y, z)
select w);
但我们的代码无法编译,因为我得到 Calculate
参数的错误 cannot convert from 'LanguageExt.Either<string, int>' to 'int'
。我应该如何链接这些函数?
x、y 和 z 不是 int 类型,而是 Either<string, int>
更改 Calculate(int x, int y, int z)
以接受 Either 调用的实例:Calculate(Either<string, int> x, Either<string, int> y, Either<string, int>z)
,或传递
x.{The int getter property}
问题是 LINQ 查询无法计算出要使用哪个版本的 SelectMany
,因为第二行中没有使用 x
。您可以通过将 Task<Either<L, R>>
转换为 EitherAsync<L, R>
:
public static async Task<int> M()
{
var res = from x in Op1().ToAsync()
from y in Op2().ToAsync()
from z in Op3().ToAsync()
from w in Calculate(x, y, z).ToAsync()
select w;
return await res.IfLeft(0);
}
或者,而不是 returning Task<Either<L, R>>
return EitherAsync<L, R>
:
public static EitherAsync<string, int> Op1() =>
1;
public static EitherAsync<string, int> Op2() =>
2;
public static EitherAsync<string, int> Op3() =>
3;
public static EitherAsync<string, int> Calculate(int x, int y, int z) =>
x + y + z;
public static async Task<int> M()
{
var res = from x in Op1()
from y in Op2()
from z in Op3()
from w in Calculate(x, y, z)
select w;
return await res.IfLeft(0);
}