"with" should be deprecated with extreme prejudice

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun May 17 18:45:00 PDT 2009


dsimcha wrote:
> == Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
>> 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.
>> Maintenance of any type that is being used with "with" becomes a very
>> dangerous proposition because it can silently change meaning of code.
>> I therefore submit that "with" is an extremely dangerous feature and
>> should be removed from the language. What say you?
>> Andrei
> 
> Absolutely not.  It's far too useful as syntactic sugar when working heavily with
> plain old data structs, and saves a lot of tedious repetition of the struct
> instance name.

Make it a member function! If you don't have access to the struct, use 
"s." as the prefix! I can't believe that's stopping you from getting 
work done!

> There are lots of similar bonehead things one could do.  For one,
> think of the opposite:  accidentally declaring a local variable in a member
> function that hides the struct- or class-level variable.  I see your point, but I
> simply think the benefits drastically outweigh the downsides.  You can pry with()
> out of my cold, dead hands.

That there are other bad things in D doesn't make "with" any more virtuous!

I think you don't entirely get my point. The "with" statement is almost 
a textbook example of bad language design, creating a mess of 
long-distance dependencies. It is terrible. I can't believe it snuck 
under our nose for so long. It's a radioactive-leaking sarcophagus that 
must be buried.


Andrei



More information about the Digitalmars-d mailing list