我想创建一个流程,该流程从传入元素中创建新源(它将是持久性查询),然后展平结果。像这样的简化示例:
var z = Source.Single(1).ConcatMany(i => Source.Single(i));
此代码按预期编译并工作。我的问题是,当我将其转换为 F# 时:
let z = Source.Single(1).ConcatMany(fun i -> Source.Single(i))
我收到一条错误消息
This expression was expected to have type
'IGraph<SourceShape<'a>,Akka.NotUsed>'
but here has type
'Source<int,Akka.NotUsed>'
我认为其原因是 F# 处理协/逆变的方式与 C# 不同,并且不能简单地转换这些通用特化 ( https://github.com/fsharp/fslang-suggestions/issues/162 ),但我无法找到在 int
之间进行转换的方法。和一个 SourceShape<int>
。是否可以将此示例转换为 F#?
最佳答案
查看 the code on GitHub ,看来Source<TOut, TMat>
是 IGraph
的直接实现,所以你应该能够转换它:
public sealed class Source<TOut, TMat> : IFlow<TOut, TMat>, IGraph<SourceShape<TOut>, TMat>
let z = Source.Single(1).ConcatMany(fun i -> Source.Single(i) :> IGraph<SourceShape<int>,Akka.NotUsed>)
我认为 C# 和 F# 使用之间最大的区别是 C# 会自动为您进行向上转换。
关于f# - 如何在 F# 中使用 Akka.Streams.*.ConcatMany?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58484223/