Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between IDelegateEvent and IEvent in F#?

Tags:

f#

The docs say:

F# gives special status to non-virtual instance member properties compatible with type IDelegateEvent, generating approriate .NET metadata to make the member appear to other .NET languages as a .NET event.

But the behavior of IDelegateEvent and IEvent appears to be the same with or without the CLIEvent attribute.

I'm using 1.9.7.8.

like image 321
Daniel Avatar asked Jan 21 '10 18:01

Daniel


2 Answers

IEvent inherits from IDelegateEvent, but puts an additional constraint on the type of delegate allowed (forcing it to return unit). IEvent also inherits from IObservable. Unless you want to have an event with a weird type, there's no reason to rely on the base IDelegateEvent interface instead of the more common IEvent.

What do you mean when you say that [<CLIEvent>] doesn't make a difference? In FSI there is a bug which prevents events from being generated correctly on the type (e.g. as seen via Type.GetEvents()), but events are generated correctly when the code is compiled. Even is FSI you should see the add_ and remove_ methods being generated when you apply the [<CLIEvent>] attribute, though.

like image 199
kvb Avatar answered Nov 15 '22 10:11

kvb


I think that in some historic versions of F#, you would use IDelegateEvent for standard .NET events and IEvent for F# events. The CLIEvent attribute was added more recently (because the original solution had quite a few issues). So, I think the IDelegateEvent is very rarely useful now (kvb mentioned some more specific differences).

A more interesting difference is between IEvent<'T> which is an event with handler Handler<'T> (which is defined somewhere in F# libraries, I believe) and IEvent<'Del, 'T> which can be used with any handlers, for example IEvent<EventHandler, EventArgs> which appears quite frequently in .NET libraries...

like image 28
Tomas Petricek Avatar answered Nov 15 '22 10:11

Tomas Petricek