Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing C# EventHandlers in F#

I have an interface, written in C#, defined as this :

public interface IWidget
{
    object Evaluate();
    event EventHandler Invalidated;
}

When I try to implement this interface in F#, I look at what F# thinks the IWidget interface is (by hovering my mouse over it), and I see

type IWidget = 
    interface
        member Evaluate : unit -> obj
    end

It appears to ignore the Invalidated event entirely... is this a known issue with F# and if so is there any way to work around it? When implementing my F# version of IWidget, can I just implement this event outside of the IWidget section or what? It seems really nasty that f# handles the "event" keyword so poorly...

UPDATE: After further fiddling around, studio was then saying things like:

'no implementation was given for IWidget.remove_Invalidate(value:EventHandler):unit'

then, when I added those methods so the whole thing looked like:

interface IWidget with
    member w.Evaluate() = new obj()
    member w.add_Invalidated(value:EventHandler) = ()
    member w.remove_Invalidated(value:EventHandler) = ()
end

it compiled fine, even though the tooltip was still saying the only member of IWidget was Evaluate()... it seems like the way F# (or at least the IDE) handles this stuff is really screwy...

ANOTHER UPDATE: According to the tooltip in the IDE, the [] tag allows an event to be compiled as a CLI metadata event, by transforming it to a pair of add_/remove_ methods... just FYI for anyone who was as confused by this as I was. In short, either implementing those two methods or using that tag work fine, though the fact that the tooltip view of the IWdiget interface lacks any mention of the Invalidate event, and the necessity of implementing such an event is only noticed when the compiler throws an error, is still a clear bug and is pretty confusing. For anyone curious, the following code works fine:

let invalidated = new DelegateEvent<System.EventHandler>()

interface IWidget with
    member w.Evaluate() = new obj()
    [<CLIEvent>]
    member w.Invalidated = invalidated.Publish
end

Thanks for all the help everyone!

like image 359
lvilnis Avatar asked Jul 15 '10 00:07

lvilnis


People also ask

What is implementing in C?

Basically, an implementation is the combination of a compiler and the C library it supports. – Jonathan Leffler. Apr 15, 2019 at 14:20. 4. C language = what you type into a text editor; implementation = the thing that actually does something with what you typed.

How was C implemented?

The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11.

Can we implement classes in C?

C Classes A class consists of an instance type and a class object: An instance type is a struct containing variable members called instance variables and function members called instance methods. A variable of the instance type is called an instance.


1 Answers

F# does support events.

For example:

let invalidated = new DelegateEvent<System.EventHandler>()

[<CLIEvent>]
member this.Invalidated = invalidated.Publish
like image 127
SLaks Avatar answered Oct 12 '22 20:10

SLaks