fsprojects / FSharp.Control.Reactive

Extensions and wrappers for using Reactive Extensions (Rx) with F#.
http://fsprojects.github.io/FSharp.Control.Reactive
Other
284 stars 60 forks source link

Is Observable.fromEventGeneric constrained too much? #167

Closed shirok1 closed 2 years ago

shirok1 commented 2 years ago

https://github.com/fsprojects/FSharp.Control.Reactive/blob/ef1d7c629a8d522abe2101a2a9b1e63038f2f242/src/FSharp.Control.Reactive/Observable.fs#L744

Writing Observable.fromEventGeneric event.AddHandler event.RemoveHandler will prompt that 'a -> unit is not implying delegate, however, System.Reactive.Linq.Observable.FromEvent(event.AddHandler, event.RemoveHandler) works just fine. I actually write my own helper function:

let toObservable (event: IEvent<'a, 'b>) : IObservable<'b> =
//    Observable.fromEventGeneric event.AddHandler event.RemoveHandler
    System.Reactive.Linq.Observable.FromEvent(event.AddHandler, event.RemoveHandler)

I wonder if this is by design.

deviousasti commented 2 years ago

The method you're referring to is specifically used to convert an delegate event of type Action. In C#, this would be declared as:

public event Action<TEventArgs> Event;

IEvent is native to F# already implements IObservable.

type IEvent<'Delegate,'Args> =
  inherit System.IObservable<'Args> inherit IDelegateEvent<'Delegate>

so unlike a .NET event delegate, to convert from an F# event to observable, a static cast is sufficient. i.e,

let toObservable (event: IEvent<'a, 'b>) : IObservable<'b> =
    event :> IObservable<_>
shirok1 commented 2 years ago

I see.