Compartilhar via


Anonymous Methods: Alternate Syntax

A few of you have suggested alternate syntaxes for anonymous methods or just complained about their syntax.  Believe it or not syntax is very heavily discuess by the language design team (of which I am only an occasioanlly invited guest).  Iterators almost got cut because they couldn't come up with a good syntax.  So far I haven't seen a single syntax that the designers didn't at least glance at.  They wanted anonymous methods to be light-weight, but still clear and mostly understandable.  I guess there weren't enough scheme or lisp people in the design group  because most of those syntaxes dead ended on picking the right open and close characters that would work.  Square brackets were out because of arrays; angle brackets we're already overloaded between shifting, comparisons, and generics; parenthesis we're too ambigous; that left curly brackets.  Once they decided on curlies, it looked too much like a method body to not have the parameter list on the outside.  The last decision to be made was 'keyword or no keyword'.  Hate me if you like, but I was in the keyword camp.  I just think the keyword makes things cleaner (not to mention prevents some ambiguities in the grammar).  But then they had to choose a keyword.  'delegate' is a good clear word and I think it helps newbies understand what's going on a little.  My only complaint is that it's so darn long!  I wish we could have found an equally expressive and unambigous keyword that was only 2 or 3 characters long... :(

--Grant

Comments

  • Anonymous
    March 18, 2004
    With Whidbey's Intellisense, typing 'delegate' is easy.

    The only thing I would change is to optionally specify the delegate type, too:

    buttonOK.OnClick += delegate Event { Close(); }

    Having the type there would make it easier to provide a richer intellisense experience.
  • Anonymous
    March 18, 2004
    Yay for keywords.

    Hey, theres 70+ keywords already, a few more won't hurt!