D's greatest mistakes

bearophile bearophileHUGS at lycos.com
Mon Nov 29 04:06:20 PST 2010


Manfred_Nowak:

> Daniel Gibson wrote:
> 
> > What's wrong with "with"?
> 
> http://www.digitalmars.com/d/archives/digitalmars/D/with_should_be_deprecat
> ed_with_extreme_prejudice_90375.html#N90375

Andrei said there:

> I think "with" is a very dangerous feature due to the way it hides 
> symbols. It essentially makes the feeblest attempt at modular reasoning 
> utterly impossible:
> 
> int x, y;
> with (whatever)
> {
>      y += x;
>      ++x;
> }
> 
> What can be said about such code? Nothing. If whatever has or will ever 
> have fields x or y or both, the names will bind to them; otherwise, 
> they'll bind to the locals. Non-local code dependency at its finest.

Today "with" is a bit tricky still, but its main problem that Andrei talks about has being patched. So this code:

struct Foo { int x, y; }
void main() {
    Foo f;
    int x, y;
    with (f) {
         y += x;
         ++x;
    }
}

Generates:

temp.d(6): Error: with symbol temp.Foo.y is shadowing local symbol temp.main.y
temp.d(6): Error: with symbol temp.Foo.x is shadowing local symbol temp.main.x
temp.d(7): Error: with symbol temp.Foo.x is shadowing local symbol temp.main.x

Bye,
bearophile


More information about the Digitalmars-d mailing list