Reactive: Time to integrate Rx with .NET

Created on 24 Apr 2020  路  6Comments  路  Source: dotnet/reactive

Feature request

Please integrate System.Reactive into .NET and Standard, and eventually integrate with the .NET runtime core and languages.

Currently, events in C# can only be targeted either within their declaration class, or with the += and -= operators.

This limitation results in plenty of tedious and redundant code, and a lot of verbosity. For example:
```c#
Observable.FromEventPattern(
h = PropertyChanged += h,
h = PropertyChanged -= h)

If we'd integrate Rx in .NET, we could end up removing such limitations, and reach the final goal where we're able to do something like the following:

```c#
EventPattern<TextChangedEventArgs> observable = myTextBox.TextChanged;

Hence:

```c#
var observable =
myTexBox
.TextChanged
.Throttle(TimeSpan.FromSeconds(.5))
.DistinctUntilChanged()
.Select(ep => (TextBox)ep.Sender))
.Select(tb => tb.Text)
.Where(text => text != null && text.Length > 3)
.Select(text => text.ToLower());

var subscription =
observable.Subscribe(text => MessgeBox.Show(text));
```

See C# Suggestion issue.
Related

Bringing Rx to C# events will make it a super powerful language, especially around UIs.

[area] Rx

Most helpful comment

F# already has this feature, so it's not too far-fetched for C# to have it.
F# converts events into an IEvent<TDelegate, TArgs> which exposes an AddHandler method, as well as the IObservable.Subscribe method.

There's an Event module that lets you compose events.

window.KeyPress 
|> Event.filter(fun evt -> evt.Code = Keyboard.Key.Escape) 
|> Event.add (fun _ -> window.Close())

But since it natively implements an observable, you can also do:

window.KeyPress |> Observable.op... |> Observable.subscribe(fun evt -> printfn "Pressed %A" evt.Code)

All 6 comments

but this is like saying "please integrate Dapper into .NET". Only the .NET team can make that kind of call.

You can't compare dotnet/reactive to StackExchange/Dapper.

F# already has this feature, so it's not too far-fetched for C# to have it.
F# converts events into an IEvent<TDelegate, TArgs> which exposes an AddHandler method, as well as the IObservable.Subscribe method.

There's an Event module that lets you compose events.

window.KeyPress 
|> Event.filter(fun evt -> evt.Code = Keyboard.Key.Escape) 
|> Event.add (fun _ -> window.Close())

But since it natively implements an observable, you can also do:

window.KeyPress |> Observable.op... |> Observable.subscribe(fun evt -> printfn "Pressed %A" evt.Code)

When the source code generators are fully implemented, it will be possible to create a generator that replaces the standard events with RX.
An example of such a generator can be found here: https://github.com/Zalutskii/Reactive-Extensions-event-generator

Thanks for your link.
There is also https://github.com/reactiveui/Pharmacist. I think @glennawatson did some work bringing source generators to Pharmacist.

The source generator is currently in our incubator org. https://github.com/reactivemarbles/ObservableEventsSourceGenerator

Was this page helpful?
0 / 5 - 0 ratings