dual with statement

monarch_dodra via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 24 13:16:51 PDT 2014


On Thursday, 24 July 2014 at 17:19:36 UTC, Jay Norwood wrote:
> I was playing around with use of the dual WITH statement.   I 
> like the idea, since it makes the code within the with cleaner.
>   Also, I got the impression from one of the conference 
> presentations ... maybe the one on the ARM debug ... that there 
> are some additional optimizations available that the compiler 
> processes the WITH statement block.
>
> Anyway, a problem I ran into was if two structures had the same 
> member names, for example struct ar.r and ar.psm in this case 
> below.   In this case, there was no way for the compiler to 
> determine from which structure to get the member.
>
> void calc_per_sec_met(ref All ar){
>
>        with (ar.r) with(ar.psm) {
>               double per_sec = proc_cyc/ref_clock;
>               d = (a+c)*per_sec;
>               e = (c==0)?0:(a+b)/c;
>        }
> }
>
> ok, so I guess I could make all the member names unique in the 
> different structures, but that's kind of ugly.
>
> Also, what happens if using two structs of the same type in a 
> WITH statement.
> Seems like something like this would help, which I believe I've 
> seen used in database queries ...
>
>  with (ar.r1 as r1) with (ar.r2 as r2){
>    auto sum = r1.a + r2.a;
>  }

Just because "with" makes it that you don't have to be explicit, 
doesn't mean you can't:

struct S {
     int i;
     int k;
}
struct T {
     int j;
     int k;
}

void main()
{
     S s;
     T t;
     int sum;
     with (s) with (t) {
         sum = i + j + s.k + t.k;
     }
}

Or did I miss something?


More information about the Digitalmars-d-learn mailing list