Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event.create in f#?

Tags:

events

f#

I'm trying to compile the source code from : Custom WPF Controls in F#

How ever this line :

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

raises an error :

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

The MSDN's Control.Event Module (F#) page does speak about such a function :

The 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 a trigger method, add two event handlers in the form of lambda expressions, and then trigger 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.")

However note that it's only mentionned in the description, not in the example.

Also, the Control.Event Module (F#) page has no reference to such a create function.

I guess it might be an old function or something, but I'm new to F# so I can't see what it should be replaced with..

like image 890
franssu Avatar asked Dec 05 '25 10:12

franssu


1 Answers

Event.create is a fairly old API for events, from before F# 2.0 judging by what's on MSDN. It gave you a trigger function and a published event - both of which now live as Publish and Trigger members of Event class.

So if you wanted to implement create in the 'modern' terms, it might look somewhat like this:

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

I don't suggest you use it universally, but perhaps that's good enough to bring that old code back to life (the correct approach here being refactoring it to use Publish and Trigger instead of create).

like image 161
scrwtp Avatar answered Dec 07 '25 04:12

scrwtp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!