"with" should be deprecated with extreme prejudice

Lester L. Martin II lestermartin92 at gmail.com
Sun May 17 20:29:58 PDT 2009


Andrei Alexandrescu Wrote:

> 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

I understand that my suggestion makes with a bubble, but I always use and have always considered its use to mainly be for accessing the members of a variable and being able to act upon (set) them. That's why I believe that my idea was good. The shadowing idea will be fine. If I'm right, you can easily just escape the scope by placing the with statement in a new function if worst comes to worst and names match in almost every scope. Or it could automagically be made a member function. 




More information about the Digitalmars-d mailing list