D's greatest mistakes

Daniel Gibson metalcaedes at gmail.com
Mon Nov 29 04:17:34 PST 2010


bearophile schrieb:
> 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
> 

I see

> 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

Great, so "with" shouldn't be much of a problem anymore. I find it really handy, 
e.g. when dealing with enums (like in a switch() statement).

Cheers,
- Daniel


More information about the Digitalmars-d mailing list