Reactive Extensions have an Observable.Start() function.
Visual Studio tells me that it
Invokes the action asynchronously surfacing the result through an observable sequence.
It has return type IObservable<Unit>
All the MDSN says about Unit is that it
Represents void.
But why do I need to represent void?
Why do I need to convert a stream of voids to list and take first element from it to replace parallel ForEach?
The Reactive Extensions (or Rx) is based very strongly in functional programming, and functional programming requires that every operator returns a value.
In imperative languages, like C#, that's not the case. We can define methods without a return type like void Foo() { ... }. In non-functional languages this is fine, when we call Foo() we are expecting it to do whatever it does and have it return control to us.
In functional languages we can call void methods as we need everything to have a return type. So for Rx to be functional we need to have a special return type that signifies void, but we can't use void or even the actual type System.Void, as the return type since the compiler won't allow it. So in comes System.Reactive.Unit as the special type to indicate that the returned value is void.
And since observables are all based on IObservable<T> (there is no IObservable without T) you must have a return type when call Observable.Start(Action). Therefore we have IObservable<Unit>.
I don't understand what you mean by, "Why do I need to convert a stream of voids to list and take first element from it to replace parallel ForEach?"
Why not
void Start(IObservable<T>, Action<T>)?
That signature isn't making sense to me. If you have an Action<T> then the overload of Observable.Start that applies is IObservable<T> Observable.Start(Action<T>). If it is Action (which is the subject of your question) then it is IObservable<Unit> Observable.Start(Action).
Are there examples where Unit is used in C#, not some other languages?
This is also a question I find hard to understand. I can't possibly know every other language in enough detail to tell you where Unit is exclusively used in C#. In fact there are so many ports of Rx to other languages that I think there wouldn't be such a usage that you're asking for. Can you clarify what you're asking?
There is a question linked asking where is
Parallel.ForEach(IObservable<T>, Action<T>). And in the answer, IObservable is queried withToList()andFirst(). Which doesn't make a lot of sense for me if its only purpose is to mutate something.
The .ToList() operator turns an IObservable<T> that returns zero or more values into an IObservable<IList<T>> that returns one value (once the source observable completes). So the following .First() just turns the IObservable<IList<T>> into a IList<T>.
It has been used by the linked answer to force the execution of the observable and to block until the observable completes. Remember that observables have the same deferred execution model as enumerables.
In functional languages,
Unitis a compromise between pure functional paradigm and real word applications. What is its purpose in C#, which has never been purely functional?
It doesn't matter how purely functional C# is or isn't, the type Unit is used to allow observables, which must be strongly-type and return zero or more values, to have a type which indicates no meaningful return type.
It is very much a parallel with the EventArgs an event handler with the signature void Foo(object sender, EventArgs e) - when the event fires you know something happened, but you get no information from the e. Getting a value of Unit from an observable is very much like getting an EventArgs in an event handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With