Neat trick - 'with' and unnamed objects + 'with' proposals
Bill Baxter
dnewsgroup at billbaxter.com
Tue Apr 24 09:05:35 PDT 2007
Dan wrote:
> Bill Baxter Wrote:
>
>> I just discovered this little trick that I think is way cool.
>> You can use 'with' in combination with an anonymous object to set
>> attributes on that object without having to bother giving it a name or a
>> variable.
>>
>>
>> with(new someGUIWidget(myParent)) {
>> shown = true;
>> focusable = false;
>> }
>
> In JavaScript they've had this "feature" for a very long time. I never, ever use it. Why?
>
> x = 3;
> myFunc = function(){
> x = 2;
> this.x = 1;
> bla bla bla ginger
> with(x){
> bla bla bla bla ginger
> bla bla bla
> bla bla
> x += 3;
> bla bla
> }
> x -= 2;
> }
>
> That in itself isn't so bad. But in real code, it looks godawful and makes a guy concentrate on scoping it for a second; distracting him from the algorithm.
You seem to be harping on 'with' in general, not with what I was
actually talking about which is using it on unnamed variables. 'with'
itself has been in D as long as I've known D, I just didn't realize you
could use it with anonymous objects.
Anyway, you certainly don't have to like it or use it. But I find it
useful in moderation.
--bb
More information about the Digitalmars-d
mailing list