"with" should be deprecated with extreme prejudice

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun May 17 18:55:24 PDT 2009


Lester L. Martin II wrote:
> Andrei Alexandrescu Wrote:
> 
>> 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
> 
> I agree with dsimcha.
> 
> When you do this, do you consider how much code written in D actually
> uses this.

That code is a bomb waiting to explode.

> I find it very, very useful.

I believe you don't understand its extreme dangers.

> It's deprecation and further
> removal would take a huge hit on my code.

I understand.

> What I would propose
> instead is that you can't modify variables outside of the scope, but
> only those that reside in the type whatever. In this case, you can't
> possibly change the variables outside of the with(whatever) scope,
> but you can modify anything that is in the type whatever, because
> this is how I believe it is mainly used. Deprecation of this feature
> should never happen. I doubt I and dsimcha will be the only unhappy
> coders.

I think that restriction would be too harsh, as it would make with 
essentially an isolated bubble. Would you be ok with Jason's suggestion 
that shadowing will be flagged? Consider:

struct Widget
{
    //int wyda;
}

void main()
{
     int wyda;
     Widget w;
     with (w)
     {
         wyda = 4;
     }
}

Would you agree to issue a compile-time error upon uncommenting the 
member of Widget.


Andrei



More information about the Digitalmars-d mailing list