dual with statement

Jay Norwood via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 24 10:19:35 PDT 2014


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;
  }




More information about the Digitalmars-d-learn mailing list