Control Structures Proposal

Reiner Pope some at address.com
Wed Jul 25 00:44:47 PDT 2007


Reiner Pope wrote:
> I like the idea of adding more allowable operator overloads -- it's a 
> relatively easy way to allow more user-defined syntaxes.
> 
I had meant to expand on this point, but I forgot about it. I wanted to 
say that operator overloads can provide syntaxes for common things 
without requiring heavy features like AST macros. I think this is a good 
thing, and I believe that enough can be done with block structures to 
warrant including them as an operator overload.

However, owing to the scoping issues I mentioned in my previous post, I 
think your approach is not quite ideal, and I think a preferable 
approach (although humbler and more limited) would be trailing 
delegates, which has been discussed a few times in the past.

Trailing delegates solve the scope issue by cheating: we say, "it is 
common to declare some variables which are accessed by the following 
block, so we will make a special-case syntax for that." By doing this, 
you get a syntax something like:

myList.each() (i)  // i is declared here, but type-inferred like foreach
// (the first parentheses may be omitted as per normal function calls)
{
     writefln(i);
}

While the normal reaction is that such special cases are bad, I would 
argue the contrary, because that is the essence of operator overloading: 
a combination of special case syntaxes can lead to a variety of useful 
results.

   -- Reiner



More information about the Digitalmars-d mailing list