Skip to content

IEvent.Publish "forgets" that it's an IObservable when used with an anonymous record argument #6572

Description

@reinux

When the argument for an event is an anonymous record, Event.Publish forgets that the IEvent it's returning is also an IObservable, requiring it to be cast to IObservable explicitly before it can be subscribed to or added to. Using Observable.add seems to work fine as well.

Of course, the workarounds are perfectly acceptable, but the type error is rather misleading and had me believing it simply can't be done.

Repro steps

let e = Event<_>()
e.Trigger {|number = 3|}
e.Publish.Add (printfn "%A")    // error

Expected behavior

e.Publish.Add should compile as the others do.

Actual behavior

error FS0193: Type constraint mismatch. The type 
    'IEvent<{|number : int|}>'    
is not compatible with type
    'IObservable<{|number : int|}>'    

Known workarounds

This works just fine when the IEvent is explicitly cast to IObservable, or when using a function rather than any of the IObservable instance methods.

(e.Publish :> IObservable<_>).Add(fun e -> printfn "Number: %d" e.number)   // works
e.Publish |> Observable.add(fun e -> printfn "Omfg: %d" e.number)   // works

Related information

F# 4.6, VS2019

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions