digitalmars.D - DUAL FEATURE REQUEST: let "with" take a struct pointer and evaluate
- downs (9/10) Dec 20 2007 The first part of the feature request is to allow "with" to take a point...
- Jarrett Billingsley (31/45) Dec 20 2007 I like the idea, but I don't know why we can't just have (1) proper stru...
- Jarrod (4/40) Dec 22 2007 I completely agree with this. Consistency really needs to be found.
- Guillaume Benny (7/26) Dec 20 2007 Already proposed the second part:
- Bill Baxter (28/40) Dec 20 2007 +1 definitely. I think I've hit that one before too and it made me go
- downs (5/16) Dec 21 2007 Actually, I tend to agree. The first one is what's really important to m...
- Simen Kjaeraas (8/17) Dec 21 2007 ll
- Daniel Keep (8/27) Dec 21 2007 Consistency demands that 'foo' only exists until the end of the while
- Bill Baxter (25/52) Dec 21 2007 Because there's no 'this' to use if you actually need the name for
- downs (5/18) Dec 22 2007 For what it's worth, I actually intended to do that.
- downs (4/17) Dec 22 2007 Arrgh!
- Simen Kjaeraas (9/35) Dec 23 2007 =
The first part of the feature request is to allow "with" to take a pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already call methods on a struct pointer as if it was dereferenced, it makes sense to also be able to use "with" on it. The second one arises from the following really cute idea ge0rg had in #d. QuoteThings this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal. What do you think? --downs<Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 42; ... }'?
Dec 20 2007
"downs" <default_357-line yahoo.de> wrote in message news:fke1ta$2bon$3 digitalmars.com...The first part of the feature request is to allow "with" to take a pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already call methods on a struct pointer as if it was dereferenced, it makes sense to also be able to use "with" on it. The second one arises from the following really cute idea ge0rg had in #d. QuoteI like the idea, but I don't know why we can't just have (1) proper struct literals for non-static struct instances and (2) initialization of heap-allocated structs at allocation time. // stack-allocated struct auto s = MyStruct { bar: 23, baz: 42 }; // heap-allocated struct auto s2 = new MyStruct { bar: 23, baz: 42 }; Or, hell, constructors and named params. Instead, we have inconsistency and ugliness: struct MyStruct { int baz, bar; } // Look at that pretty struct initializer! static MyStruct s = { bar: 23, baz: 42 }; // Is it a function? Is it a call to opCall? Why // can't I name the members or initialize them out of // order or leave some uninitialized? Why is this // so different from the static case? auto s2 = MyStruct(42, 23); // NOW what?! Now I have an instance on the heap that's // filled with default values.. how do I initialize it? // I have to separate out the initialization into some // method function and call it as a separate step. auto s = new MyStruct; // And again I can't name anything. s.initialize(42, 23); Dumb, dumb, dumb. Don't even get me started on the "blessing" of static opCall. Are we *trying* to make stupid special cases?Things this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal. What do you think? --downs<Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 42; ... }'?
Dec 20 2007
On Thu, 20 Dec 2007 12:11:04 -0500, Jarrett Billingsley wrote:I like the idea, but I don't know why we can't just have (1) proper struct literals for non-static struct instances and (2) initialization of heap-allocated structs at allocation time. // stack-allocated struct auto s = MyStruct { bar: 23, baz: 42 }; // heap-allocated struct auto s2 = new MyStruct { bar: 23, baz: 42 }; Or, hell, constructors and named params. Instead, we have inconsistency and ugliness: struct MyStruct { int baz, bar; } // Look at that pretty struct initializer! static MyStruct s = { bar: 23, baz: 42 }; // Is it a function? Is it a call to opCall? Why // can't I name the members or initialize them out of // order or leave some uninitialized? Why is this // so different from the static case? auto s2 = MyStruct(42, 23); // NOW what?! Now I have an instance on the heap that's // filled with default values.. how do I initialize it? // I have to separate out the initialization into some // method function and call it as a separate step. auto s = new MyStruct; // And again I can't name anything. s.initialize(42, 23); Dumb, dumb, dumb. Don't even get me started on the "blessing" of static opCall. Are we *trying* to make stupid special cases?I completely agree with this. Consistency really needs to be found. Perhaps named parameters should be optional for ease, but aside from that I would really like to see this idea implemented.
Dec 22 2007
downs wrote:The first part of the feature request is to allow "with" to take a pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already call methods on a struct pointer as if it was dereferenced, it makes sense to also be able to use "with" on it. The second one arises from the following really cute idea ge0rg had in #d. QuoteAlready proposed the second part: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=57615 It's a bit like the "returning" method from Ruby on Rails: http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-returning Only BCS voted with a small "vote += 0.25;" :) Guillaume B.Things this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal. What do you think? --downs<Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 42; ... }'?
Dec 20 2007
downs wrote:The first part of the feature request is to allow "with" to take a pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already call methods on a struct pointer as if it was dereferenced, it makes sense to also be able to use "with" on it.+1 definitely. I think I've hit that one before too and it made me go huh? wha? By the way same sort of huh? wha? comes from trying to define a structs opCmp to take a ref parameter.The second one arises from the following really cute idea ge0rg had in #d. QuoteThings this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal.<Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 42; ... }'?What do you think?I'm -1 on that. This is really not so hard to do: auto foo = new myStruct; with (foo) { ... } And the new syntax only saves typing "foo" once. Not worth it. with() definitely could be a more useful beastie than it is, but I don't think that's the way, and that would just get in the way of what I think would be more useful growth opportunities like multiple with() with(auto foo=myStruct.accesor(); auto bar=yourStruct.thingy) { } or non-scoped with(), with big_object; //like c++ "using", or alias big_object <.current namespace>; or selective with(). with(something : member1,member2) { member1 = 1; // gets something.member1 member2 = 2; // gets something.member2 member3 = 3; // error - didn't import member3 } Or maybe those duties could be given to alias or import. I don't really care which, but with seems like the current underachiever of the bunch, so it seems like he could stand some more duties. --bb
Dec 20 2007
Bill Baxter wrote:downs wrote:Actually, I tend to agree. The first one is what's really important to me; the second is more like a "wouldn't it be neat if" thing. Personally, I think it's kinda hard to understand as well. Makes me wonder why I proposed it in the first place. Eh, well :) --downsThe first part of the feature request is to allow "with" to take a pointer to a struct as a parameter.+1 definitely. I think I've hit that one before too and it made me go huh? wha?I'm -1 on that.<Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 42; ... }'?
Dec 21 2007
On Thu, 20 Dec 2007 16:31:56 +0100, downs <default_357-line yahoo.de> = wrote:The first part of the feature request is to allow "with" to take a =pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already ca=llmethods on a struct pointer as if it was dereferenced, it makes sense =toalso be able to use "with" on it. The second one arises from the following really cute idea ge0rg had in==#d. Quotebaz =3D =<Ge0rG> so you want 'auto foo =3D with(new myStruct) { bar =3D 23; =Wouldn't it be more logical to use with (auto foo =3D new myStruct) { bar =3D 23; baz =3D 42; ... }42; ... }'?
Dec 21 2007
Simen Kjaeraas wrote:On Thu, 20 Dec 2007 16:31:56 +0100, downs <default_357-line yahoo.de> wrote:Consistency demands that 'foo' only exists until the end of the while block. What, then, would be the point of giving the struct a name for the duration of a construct whose primary purpose is to allow you to omit the name? Being able to use control structures as expressions is a damn cool idea; I just don't get why more people don't like it. -- DanielThe first part of the feature request is to allow "with" to take a pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already call methods on a struct pointer as if it was dereferenced, it makes sense to also be able to use "with" on it. The second one arises from the following really cute idea ge0rg had in #d. QuoteWouldn't it be more logical to use with (auto foo = new myStruct) { bar = 23; baz = 42; ... }<Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz= 42; ... }'?
Dec 21 2007
Daniel Keep wrote:Simen Kjaeraas wrote:Because there's no 'this' to use if you actually need the name for something, like to pass it to a function. For example in DFL code you might want something like: with(auto btn = new Button) { text = "Press Me"; ... common_button_init(btn); allButtons ~= btn; }On Thu, 20 Dec 2007 16:31:56 +0100, downs <default_357-line yahoo.de> wrote:Consistency demands that 'foo' only exists until the end of the while block. What, then, would be the point of giving the struct a name for the duration of a construct whose primary purpose is to allow you to omit the name?The first part of the feature request is to allow "with" to take a pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already call methods on a struct pointer as if it was dereferenced, it makes sense to also be able to use "with" on it. The second one arises from the following really cute idea ge0rg had in #d. QuoteWouldn't it be more logical to use with (auto foo = new myStruct) { bar = 23; baz = 42; ... }<Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz= 42; ... }'?Being able to use control structures as expressions is a damn cool idea; I just don't get why more people don't like it.That is a good point. The given example just wasn't a very good one because it could easily be replaced by something 99% as succinct. But if you talk about passing the thing to a function, the expression version would be pretty nifty: create_form( with(new Button){ text="foo"; }, with(new Checkbox) { text="foo"; check = true; } ... ) --bb
Dec 21 2007
Daniel Keep wrote:Consistency demands that 'foo' only exists until the end of the while block. What, then, would be the point of giving the struct a name for the duration of a construct whose primary purpose is to allow you to omit the name?For what it's worth, I actually intended to do that. Consider:R delegate() bind(R, T...)(R delegate(T) dg, T bind) { struct Holder { typeof(dg) _dg; T _bind; R call() { return _dg(_bind); } } with (new holder) { _dg=dg; _bind=bind; return &call; } }Luckily D 2.0 will make this unnecessary :) --downs
Dec 22 2007
downs wrote:Consider:Arrgh! I meanR delegate() bind(R, T...)(R delegate(T) dg, T bind) { struct Holder { typeof(dg) _dg; T _bind; R call() { return _dg(_bind); } } with (new holder) { _dg=dg; _bind=bind; return &call; } }with (new Holder) { foreach (id, entry; dg) _dg[id]=entry;Sorry :)
Dec 22 2007
On Sat, 22 Dec 2007 02:15:27 +0100, Daniel Keep = <daniel.keep.lists gmail.com> wrote:Simen Kjaeraas wrote:On Thu, 20 Dec 2007 16:31:56 +0100, downs <default_357-line yahoo.de>==wrote:The first part of the feature request is to allow "with" to take a pointer to a struct as a parameter. This is for the sake of internal consistency; since you can already =e =call methods on a struct pointer as if it was dereferenced, it makes sens=into also be able to use "with" on it. The second one arises from the following really cute idea ge0rg had =; baz#d. Quote<Ge0rG> so you want 'auto foo =3D with(new myStruct) { bar =3D 23=Consistency demands that 'foo' only exists until the end of the while block. What, then, would be the point of giving the struct a name for=Wouldn't it be more logical to use with (auto foo =3D new myStruct) { bar =3D 23; baz =3D 42; ... }=3D 42; ... }'?the duration of a construct whose primary purpose is to allow you to omit the name?Right. Make that static with (auto foo =3D new myStruct) { bar =3D 23; baz =3D 42; ... }= :p
Dec 23 2007