www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [maintenance] `with' is an annoyance

reply Manfred Nowak <svv1999 hotmail.com> writes:
From the theoretical view I know, that using `with'-statements 
complicatates every proof of correctness tremendously.

From the practical view I know, that detecting a `with'-statement in 
foreign code let me shudder, because I have to remember in full 
precision the name space, that is opened by the `with'-statement.

Nested `with'-statements let me have visions of devils coming out of 
hell.

D has a `with'-statment. I do not like it. I would prefer to code 
something like this:
<example>
  {
     alias complicated.expressionYou[Can[Imagine]].betterThanMe al;

     al.i= j + al.k;
  }
</example>

But such aliases ( or mixins?) are not possible in D.

Moreover: together with the overloading of operators `with'-
statements are somehow useless, like this example shows, when putting 
an array into a class:
<code>
void main(){
  struct Entry{
    int variable;
  }
  Entry[10] table;

  with( table[ 1]) variable=0;

  class Class{
    struct Entry{
      int variable;
    }
    Entry[10] table;
    Entry opIndex( int inx){
      return table[ inx];
    }
  }
  Class c= new Class;

  with( c[ 1]) variable= 0;
}
</code>

Did you see the "c.opIndex(1) is not an lvalue" error at first 
glance?

-manfred
Feb 03 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
[snip]
  class Class{
    struct Entry{
.      int variable;
    }
    Entry[10] table;
    Entry opIndex( int inx){
      return table[ inx];
    }
 }
  Class c= new Class;

  with( c[ 1]) variable= 0;
 }
 </code>

 Did you see the "c.opIndex(1) is not an lvalue" error at first
 glance?
That looks like a compiler bug to me. The doc says with(expr) { ... ident ... } is semantically the same as {Object tmp; tmp = expr; ... tmp.ident ...}
Feb 03 2005
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
"Ben Hinkle" wrote:

 That looks like a compiler bug to me. The doc says
 with(expr) { ... ident ... }
 is semantically the same as
 {Object tmp; tmp = expr; ... tmp.ident ...} 
But if the docs are right, then the semantic is wrong, because every assignment is made to the `tmp'-object and not forwarded to the expression. -manfred
Feb 03 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Manfred Nowak" <svv1999 hotmail.com> wrote in message 
news:cttta3$fs7$1 digitaldaemon.com...
 "Ben Hinkle" wrote:

 That looks like a compiler bug to me. The doc says
 with(expr) { ... ident ... }
 is semantically the same as
 {Object tmp; tmp = expr; ... tmp.ident ...}
But if the docs are right, then the semantic is wrong, because every assignment is made to the `tmp'-object and not forwarded to the expression. -manfred
The expression is only evaluated once before the body is executed. So I think for your example with(c[1]) variable = 0; should be the same as {Class tmp; tmp = c[1]; tmp.variable = 0; } Since objects have reference semantics the variable tmp shares the reference with c[1].
Feb 03 2005
next sibling parent "Ben Hinkle" <bhinkle mathworks.com> writes:
 {Class tmp; tmp = c[1]; tmp.variable = 0; }
ack - that should be Entry - not Class. I hit send without double checking.
Feb 03 2005
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
"Ben Hinkle" wrote: 

[...]
 Since objects have reference semantics the variable tmp shares the
 reference with c[1]. 
I see. Agreed. -manfred
Feb 03 2005