Feature request: extending comma operator's functionality

Tommi tommitissari at hotmail.com
Fri Oct 5 06:22:01 PDT 2012


On Friday, 5 October 2012 at 07:26:43 UTC, Tove wrote:
> Don't forget the with statement, it's not "just" for switches! 
> In many cases it's actually even better than the proposed 
> changes _and_ it works today!
>
> import std.stdio;
>
> struct d_is_beautiful
> {
>   int a=1;
>   int b=2;
> }
>
> void main()
> {
>   with(d_is_beautiful()) if(a==1)
>     writeln("ok");
>   else
>     writeln("ko:", a);
>
>   with(d_is_beautiful()) do
>   {
>     ++a;
>     writeln("iter");
>   }
>   while(a!=b);
> }

This was news to me. It seems you can also use a tuple for that. 
That's a pretty decent workaround:

import std.typecons;
//...

with (Tuple!(int, "a")(getInt()))
if (a > 9)
{
     //...
}
else with (Tuple!(char, "b")(getChar()))
if (b == 'D')
{
     //...
}


On Friday, 5 October 2012 at 07:28:29 UTC, Timon Gehr wrote:
>> But if there are else-if clauses, then you end up polluting 
>> your namespace, and notice how the syntax of your workaround 
>> deteriorates exponentially:
>> ...
>
> Check your math.

Correction: "the syntax deteriorates only linearly".


More information about the Digitalmars-d mailing list