Event.create in f #?

I am trying to compile from source: WPF Custom Controls in F #

As always, this line:

 let (handler, event) = Event.create<EventArgs>()

      

raises an error:

The value, constructor, namespace or type 'create' is not defined

The MSDN page Control.Event Module (F #) talks about such a function:

Additional functionality provided by the Event module is illustrated here. The following code example illustrates the basic use of Event.create to create an event and trigger method, add two event handlers in the form of lambda expressions, and then fire the event to execute both lambda expressions.

type MyType() =
   let myEvent = new Event<_>()

   member this.AddHandlers() =
      Event.add (fun string1 -> printfn "%s" string1) myEvent.Publish
      Event.add (fun string1 -> printfn "Given a value: %s" string1) myEvent.Publish

   member this.Trigger(message) =
      myEvent.Trigger(message)

let myMyType = MyType()
myMyType.AddHandlers()
myMyType.Trigger("Event occurred.")

      

Note, however, that this is only mentioned in the description and not in this example.

Also, the Control.Event Module (F #) page is irrelevant for such a function create

.

I'm guessing it might be an old function or something, but I'm new to F #, so I can't see it needs to be replaced ..

+3


source to share


1 answer


Event.create

is a pretty old API for events since F # 2.0, judging by what is on MSDN. It gave you a trigger function and a posted event - both of which now live as members Publish

and Trigger

classes Event

.

So, if you want to implement create

in "modern" terms, it might look something like this:



module Event = 
    let create<'T> () = 
        let event = Event<'T>()
        event.Trigger, event.Publish

      

I don't suggest using it all over the place, but perhaps good enough to revert this old code (the correct approach here is to refactor it Publish

and use it Trigger

instead create

).

+8


source







All Articles