With the new Async support using C # 7.0 you can create an IObservable <T> generator

C # 7 is missing, and I see that they opened the async keywords expect to resolve whatever you want. However, the surface area of ​​the API seems very large , and I'm not sure about the limitations. In particular, I wonder if this is possible.

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); } } 

which, I think, is equivalent

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

There is a link to Reddit from a while back discussing why async waiting can be as powerful as f # evaluation expressions, if they were allowed to be.

+5
source share
1 answer

I would have guessed (for now):

The compiler must somehow stitch your code into a specific type to build and return. IObservable<T> not a type, so the compiler cannot create it.

Also from 7.0 Release Notes :

A new language feature means that async methods can return other types in addition to task, task, and void. The return type should still satisfy the asynchronous pattern, that is, the GetAwaiter method should be available.

This is not to say that someone could not build an ObservableTask<T> or something that would implement IObservable<T> , as well as fulfill the requirements of the Task API. This has not been done (yet).

You still have a mismatch problem: Task<T> revolves around returning 0-1 T objects, IObservable<T> returns 0-n T objects.

0
source

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


All Articles