"with" should be deprecated with extreme prejudice

Bill Baxter wbaxter at gmail.com
Mon May 18 15:43:05 PDT 2009


On Mon, May 18, 2009 at 2:56 PM, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:
> Bill Baxter wrote:
>>
>> On Mon, May 18, 2009 at 1:36 PM, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>>
>>> That's not an "equiv of". It's "completely missing the point of". Each
>>> "using" costs one new scope and one level of indentation which makes it
>>> non-scalable. Indentation is *expensive*. I think the C# folks missed the
>>> class when try/catch/finally showed with extensive examples just how
>>> expensive extra indentation is.
>>
>> Oh.  Is that the only thing that bothers you about it?
>> That doesn't bother me much since it's basically for things you only
>> want to hold on to for a short span.  You should either do your
>> business in a few lines of code and get out of there, or if you can't
>> then make it a function.   Anyway, if the indentation bugs you, you
>> can always set emacs up not to indent on "using" blocks.
>
> It's huge! In TDPL, as soon as I show the expansion of two scope(exit)
> statements, code became unreadable. No wonder today's software has so
> crappy error handling and state leaks: languages seem to make it as hard
> as possible to write correct transactional code.

I agree that scope(exit) is a more general solution that allows for
more interesting tricks.
I think I'm only disagreeing with you on the magnitude of the lameness.


You can always create a new scope if you want one, but you can't
remove a scope if it is defined to be a part of the construct.  So to
me it seems clear that not building the scope into the construct is
the way to go.   This is also a strike against D's current with(),
which also forces a new scope whether you want it or not.

---
In related news, scope(exit) is not without it's own issues.  Mainly,
this doesn't seem nice to me:

void foo() {
   auto x = new Thing;
   scope(exit) x.doADance();
   // bunch of code
   x = otherThing;
   // more code
}

This will happily doADance on otherThing instead of the original x
when it exits.  I'm guessing the C# using / Python with are safer wrt
re-assignment, but I have not investigated this.  Anyone know?

--bb



More information about the Digitalmars-d mailing list