digitalmars.D - Re: Neat trick - 'with' and unnamed objects + 'with' proposals
- Dan <murpsoft hotmail.com> Apr 24 2007
- Bill Baxter <dnewsgroup billbaxter.com> Apr 24 2007
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.That's really nifty for GUI code. This could be even more useful if there were some kind of 'this' for 'with' blocks. Then you could also call non-member functions.
Lions and tigers and bears, oh my. Can't you just: alias supercalafragilisticexpialadocious.bob.mom.mom.niece.daughter d; d.shown = true; d.focusable = false; etc.?with(new someGUIWidget(myParent)) { shown = true; focusable = false; some_global_function(this); } Or perhaps analogous to how constructors are called 'this()' and have a 'this' pointer, we'd have a 'with' pointer so that the meaning of 'this' in a class wouldn't be shadowed: with(new someGUIWidget(myParent)) { some_global_function(with); }
Eww... ambiguity and context sensitivity.And even cooler would be if you could have a 'with-expression' that just evaluates to the thing in the parens: auto obj = with(new someGUIWidget(myParent)) { shown = true; focusable = false; some_global_function(this); };
auto obj = new someGuiWidget(myParent){ shown = true; focusable = false; }; some_global_function(obj); <- can't execute in a class declaration. Try in the constructor?Sometimes I've seen people try to make APIs in C++ where all the attribute setters return the object so that property setting can be chained, like: myObject.set_focusable(true).set_shown(true);
I did that back in the day too, but not for setting properties, it was for performing more complex algorithms with the DOM, such as making an object move in circles, or fall using a gravity formula.Presumably the objective is to avoid repeating the name of the object a lot. But that never works out too well, because making every mutator return a pointer to 'this' is just not so practical in the end, and forcing every property setting operation to use function call syntax is also not so great.
Property setting operations? Totally agree. Returning a this* isn't so bad unless you have something else to return. I only did it with subroutines. The real reason it's bad is because you're masturbating the stack doing calls like that just to avoid doing something in source code. Such a thing is forgiveable in web scripts where filesize matters, it's not in programming.
Apr 24 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
Apr 24 2007