www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DUAL FEATURE REQUEST: let "with" take a struct pointer and evaluate

reply downs <default_357-line yahoo.de> writes:
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. Quote

 <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 42; ...
}'?
Things this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal. What do you think? --downs
Dec 20 2007
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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. 
 Quote

 <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 
 42; ... }'?
Things this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal. What do you think? --downs
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?
Dec 20 2007
parent Jarrod <qwerty ytre.wq> writes:
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
prev sibling next sibling parent Guillaume Benny <guillaume.spam.remove_me.spam.benny sympatico.ca> writes:
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.
 Quote
 
 <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz =
 42; ... }'?
Things this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal. What do you think? --downs
Already 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.
Dec 20 2007
prev sibling next sibling parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
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.
Quote
 
 <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz = 42; ...
}'?
Things this breaks: * None; as usual with my feature requests, it only affects behavior that is currently illegal.
 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
parent downs <default_357-line yahoo.de> writes:
Bill Baxter wrote:
 downs wrote:
 The 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?
 <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz =
 42; ... }'?
I'm -1 on that.
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 :) --downs
Dec 21 2007
prev sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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=
ll
 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. Quote

 <Ge0rG> so you want 'auto foo =3D with(new myStruct) { bar =3D 23; =
baz =3D =
 42; ... }'?
Wouldn't it be more logical to use with (auto foo =3D new myStruct) { bar =3D 23; baz =3D 42; ... }
Dec 21 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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 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. Quote

 <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz
= 42; ... }'?
Wouldn't it be more logical to use with (auto foo = new myStruct) { bar = 23; baz = 42; ... }
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. -- Daniel
Dec 21 2007
next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
Daniel Keep 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 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. Quote

 <Ge0rG> so you want 'auto foo = with(new myStruct) { bar = 23; baz
= 42; ... }'?
Wouldn't it be more logical to use with (auto foo = new myStruct) { bar = 23; baz = 42; ... }
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?
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; }
 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
prev sibling next sibling parent reply downs <default_357-line yahoo.de> writes:
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
parent downs <default_357-line yahoo.de> writes:
downs wrote:
 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;
 	}
 }
Arrgh! I mean
 with (new Holder) {
 	foreach (id, entry; dg) _dg[id]=entry;
Sorry :)
Dec 22 2007
prev sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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 =
=
 call
 methods on a struct pointer as if it was dereferenced, it makes sens=
e =
 to
 also be able to use "with" on it.

 The second one arises from the following really cute idea ge0rg had =
in
 #d. Quote

 <Ge0rG> so you want 'auto foo =3D with(new myStruct) { bar =3D 23=
; baz
 =3D 42; ... }'?
Wouldn't it be more logical to use with (auto foo =3D new myStruct) { bar =3D 23; baz =3D 42; ... }
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?
Right. Make that static with (auto foo =3D new myStruct) { bar =3D 23; baz =3D 42; ... }= :p
Dec 23 2007