Custom control flow statements

Mikola Lysenko mclysenk at mtu.edu
Tue Feb 13 10:07:42 PST 2007


Michiel wrote:
> I was thinking it might be useful if you could create custom control flow
> statements like:
> 
> foreach_random (element, array) { ... }
> 
> Might just be an idea for the future of D. It could be declared like this:
> 
> void foreach_random(T)(T element, T[] array, void delegate() statements) { ... }
> 
> It would work kind of like those array functions, where the first array
> parameter can also be placed in front of the function with a . inbetween. In
> this case the last delegate parameter can also be placed after (without a .).
> 
> In theory you could also create things like:
> 
> threeway (x, 3) { ... } { ... } { ... }
> 
> What do you think?

To an extent, this is already possible.  DCSP does this for stuff like 
parallel code blocks and alternative statements.

http://assertfalse.com

However, any syntactic sugar like this needs to be very carefully 
thought out.  Consider the following:

void a()
{
	writefln("starting");

	{
		scope(exit) writefln("blah");
	}
}

This is valid code and there are many times where something like this is 
needed.  Unfortunately, if we implement what you suggest, then the 
second block would get convereted to a delegate and passed as a variadic 
argument to writefln!

-Mik



More information about the Digitalmars-d mailing list