C# 有了C7.0新的TaskLike异步支持,就可以编写IObservable生成器了

C# 有了C7.0新的TaskLike异步支持,就可以编写IObservable生成器了,c#,asynchronous,async-await,system.reactive,c#-7.0,C#,Asynchronous,Async Await,System.reactive,C# 7.0,C7出局了,我看到他们已经打开了async Wait关键字来允许您想要的任何东西。然而,API的表面积似乎很小,我不确定其局限性 具体来说,如果可能的话,我很感兴趣 int RandomNumber(){..} public async IObservable<int> Generate(){ while(true){ await Observable.Timer(TimeSpan.FromSeconds(1)).Select(_=>RandomNum

C7出局了,我看到他们已经打开了async Wait关键字来允许您想要的任何东西。然而,API的表面积似乎很小,我不确定其局限性

具体来说,如果可能的话,我很感兴趣

int RandomNumber(){..}

public async IObservable<int> Generate(){
    while(true){
        await Observable.Timer(TimeSpan.FromSeconds(1)).Select(_=>RandomNumber());

        await Observable.Timer(TimeSpan.FromSeconds(2)).Select(_=>10);
    }
}
我认为这相当于

public IObservable<int> Generate(){
   return Observable.Timer(TimeSpan.FromSeconds(1)).Select(_=>RandomNumber())
    .Concat(Observable.Timer(TimeSpan.FromSeconds(2)).Select(_=>10))
    .Replay();

}

ago上有一个链接讨论了为什么async await可以像f计算表达式一样强大,只要它们被允许就行了。

我想现在不行:

编译器必须以某种方式将代码缝合成具体的类型来构造和返回。IObservable不是类型,因此编译器无法构造它

此外,从7.0开始:

新的语言特性意味着异步方法可能返回其他 除Task、Task和void之外的其他类型。返回的类型必须是 仍然满足异步模式,这意味着必须使用GetAwaiter方法 容易接近

这并不是说有人不能构建一个ObservieTask或一些能够实现IObservable并满足任务API要求的东西。只是还没做完

您仍然会遇到不匹配的问题:任务围绕返回0-1t对象展开,IObservable返回0-ntt对象