if(;){} opinion

Jarrett Billingsley kb3ctd2 at yahoo.com
Sun Feb 26 10:47:32 PST 2006


"Georg Wrede" <georg at nospam.org> wrote in message 
news:4401ECE3.4030207 at nospam.org...
> Better 'fess up front: the intent of this post is to once and for all 
> murder, pulverize and extradite the new if construct.
>
> Consider:
>
>         if (m; std.regexp.search("abcdef", "b(c)d"))
>         {
>             writefln("[%s]", m.pre);      // prints [a]
>             writefln("[%s]", m.post);     // prints [ef]
>             writefln("[%s]", m.match(0)); // prints [bcd]
>             writefln("[%s]", m.match(1)); // prints [c]
>             writefln("[%s]", m.match(2)); // prints []
>         }
>
> Flauting this around has shown that experienced programmers have a hard 
> time figuring out what is going on here.
>
> Consider:
>
>         if (m; std.regexp.search("abcdef", "b(c)d"))
>
> Most everybody take it for granted that here is a typo, the '(m;' must be 
> the result of a sloppy copy-paste.
>
> And in the previous case the theories ranged from all kinds of 
> behind-the-scenes magic.
>
> Now consider:
>
>         if (Regexp m = std.regexp.search("abcdef", "b(c)d"))
>         {
>             writefln("[%s]", m.pre);      // prints [a]
>             writefln("[%s]", m.post);     // prints [ef]
>             writefln("[%s]", m.match(0)); // prints [bcd]
>             writefln("[%s]", m.match(1)); // prints [c]
>             writefln("[%s]", m.match(2)); // prints []
>         }
>
> Flaunting this around (to both the original programmers, and also to 
> virgin victims), gave the same, _immediate_ comment from everybody: "Ah, 
> that's neat!"

I can see where the new syntax came from:

foreach(Foo x; a)

But I'll agree with you that it doesn't seem to make much sense in the 
context of an if statement.

I remember a while ago someone posting a suggestion that the ability to 
declare and initialize variables in conditional statements be extended from 
just for (and foreach) to all kinds of statements, such as your suggestion.

I do like the "if(type ident = expr)" syntax. 





More information about the Digitalmars-d mailing list