Friday, November 14, 2014

Events


I like C# a lot, probably as much as I dislike Java, but that's just personal taste. However, I never liked events in C#, frankly, most of the time I can't even remember the proper syntax. Here it is:

        public delegate void FinishedHandler(int x, int y);
      public event FinishedHandler FinishedEvent;

        ....

        FinishedEvent += method;

That's a little convoluted considering a new concept delegate is used just to indicate the signature of the event. Also, the += operator does not convey its intended meaning particularly well. I do, however, understand the thinking behind this and its usefulness. But there should be an easier way if we are to prototype freely. So here is the xs syntax for events, which works already on the site and augments the traditional C# syntax:

       public event FinishedEvent(int x, int y);

As declarations go, this is much clear in my opinion. There is only one construct event representing the concept and it already includes its signature. Conversely, adding implementation is much more meaningful in xs:

        on  FinishedEvent(int x, int y)
       {
            ...
       }

Here the intention is clear to add a handler to the finished event instead of "adding" (+=) a method to a property of type delegate.But there's more! since the xs compiler knows you are implementing an event it knows the signature of that event and can deduct the parameters, its types, etc. As such, all these are valid xs event implementations:

        on  FinishedEvent(x, y)
        on  FinishedEvent(int y)
        on  FinishedEvent()
Which might not be great but its neat and in the spirit of the prototyping freedom we are looking to provide. And just to top it off I went creative (never a good thing) and allowed event name matiching. At the end of the day, if you have a FinishedEvent the following code should be understandable:

        on  finished(x, y)

Too far?

1 comment:

  1. Its a great point of view and interesting reflection. In particular, i don't like C#, so I have coded only a little on c#, because its not my favour. In my modest and little incursions in this program language I encounter this tricky handler for use events. So... thumb up! for your proposal.

    ReplyDelete