digitalmars.D - member arguments in D?
- Penguin (16/16) Apr 26 2009 What do you think about:
- Daniel Keep (6/32) Apr 26 2009 I don't know that saving a few (very simple) lines of code is worth
- Nick Sabalausky (52/83) Apr 26 2009 I just happen to already have such a thing in my bag-o-tricks. See
- downs (8/64) Apr 26 2009 I _also_ have something for this for Phobos (in tools.base), but it's in...
- DisForDave (3/71) Apr 26 2009 but this is something the compiler should really be dealing with, it's s...
- downs (3/75) Apr 26 2009 But that's the point - the language is powerful enough that the hacks wo...
- grauzone (2/2) Apr 26 2009 Do people really like such code and not cringe over how unclean and
- DisForDave (3/5) Apr 26 2009 it's not just you - i agree totally. If i wanted inelegant hacks, i'd be...
- Steven Schveighoffer (17/48) Apr 27 2009 There are three benefits I see to the proposal:
- Daniel Keep (22/42) Apr 27 2009 True, but...
- Steven Schveighoffer (11/43) Apr 27 2009 Just like:
- carlos smith (3/9) Apr 26 2009 Why the keyword ?
- bearophile (5/7) Apr 26 2009 Please, lets not mud the language too much for a small syntax gain.
- Jarrett Billingsley (4/8) Apr 26 2009 *snort*
What do you think about: class Foo { int a; float b; this( member a, member b ) { } } instead of: class Foo { int a; float b; this( int a, float b ) { this.a = a; this.b = b; } }
Apr 26 2009
Penguin wrote:What do you think about: class Foo { int a; float b; this( member a, member b ) { } } instead of: class Foo { int a; float b; this( int a, float b ) { this.a = a; this.b = b; } }I don't know that saving a few (very simple) lines of code is worth adding a new keyword and magic behaviour to ctor arguments. If you really wanted to just do that, you could probably write a mixin to take care of it. -- Daniel
Apr 26 2009
"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message news:gt159p$1bq0$1 digitalmars.com...Penguin wrote:I just happen to already have such a thing in my bag-o-tricks. See attachment. Usage: ---- mixin(initMember!(someVar)); mixin(initMember!(a, b, c)); ---- Turns Into: ---- this.someVar = someVar; this.a = a; this.b = b; this.c = c; ---- begin 666 initMember.d M87( /2!S;VUE5F%R.PT*=&AI<RYA(#T M+F, /2!C.PT*+2TM+0T**B\-"G1E;7!L871E(&EN:71-96UB97(H=F%R:6%B M;&5S+BXN*0T*>PT*"6-O;G-T(&-H87);72!I;FET365M8F5R(#T 7VEN:71- M96UB97)&<F]M(2 B(BP =F%R:6%B;&5S*3L-" DO+W!R86=M82AM<V<L(")I M<CL-" D)=&AI<RYA,2 ](&-O<'E/9BYA,3L-" D)=&AI<RYB(#T 8V]P>4]F M96UP;&%T92!I;FET365M8F5R1G)O;2AA;&EA<R!F<F]M+"!V87)I86)L97,N M96UB97)&<F]M(2AF<F]M+G-T<FEN9V]F('X (BXB+"!V87)I86)L97,I.PT* M+R\)<')A9VUA*&US9RP (FEN:71-96UB97)&<F]M.B B('X :6YI=$UE;6)E M;2AC:&%R6UT 9G)O;2P =F%R:6%B;&5S+BXN*0T*>PT*"7-T871I8R!I9BAV M87)I86)L97,N;&5N9W1H(#T M:71-96UB97)&<F]M(#T-" D)"2)T:&ES+B)^=F%R:6%B;&5S6S!=+G-T<FEN M"0E^(%]I;FET365M8F5R1G)O;2$H9G)O;2P =F%R:6%B;&5S6S$N+B1=*3L- M"B\O"0EP<F%G;6$H;7-G+" B7VEN:71-96UB97)&<F]M.EQN(B!^(%]I;FET ` endWhat do you think about: class Foo { int a; float b; this( member a, member b ) { } } instead of: class Foo { int a; float b; this( int a, float b ) { this.a = a; this.b = b; } }I don't know that saving a few (very simple) lines of code is worth adding a new keyword and magic behaviour to ctor arguments. If you really wanted to just do that, you could probably write a mixin to take care of it.
Apr 26 2009
Nick Sabalausky wrote:"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message news:gt159p$1bq0$1 digitalmars.com...I _also_ have something for this for Phobos (in tools.base), but it's intended for simple constructors. class Foo { int a; float b; mixin This!("a, b"); } Or to add some instruction: mixin This!("a; #b = 0f; "); Or to add a super call and defaults: mixin This!("a, super(b=0f)");Penguin wrote:I just happen to already have such a thing in my bag-o-tricks. See attachment. Usage: ---- mixin(initMember!(someVar)); mixin(initMember!(a, b, c)); ---- Turns Into: ---- this.someVar = someVar; this.a = a; this.b = b; this.c = c; ----What do you think about: class Foo { int a; float b; this( member a, member b ) { } } instead of: class Foo { int a; float b; this( int a, float b ) { this.a = a; this.b = b; } }I don't know that saving a few (very simple) lines of code is worth adding a new keyword and magic behaviour to ctor arguments. If you really wanted to just do that, you could probably write a mixin to take care of it.
Apr 26 2009
downs Wrote:Nick Sabalausky wrote:but this is something the compiler should really be dealing with, it's such a common programming practice (the fact so many people have their own hacks for it is testament to this) i think it's worth a new keyword"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message news:gt159p$1bq0$1 digitalmars.com...I _also_ have something for this for Phobos (in tools.base), but it's intended for simple constructors. class Foo { int a; float b; mixin This!("a, b"); } Or to add some instruction: mixin This!("a; #b = 0f; "); Or to add a super call and defaults: mixin This!("a, super(b=0f)");Penguin wrote:I just happen to already have such a thing in my bag-o-tricks. See attachment. Usage: ---- mixin(initMember!(someVar)); mixin(initMember!(a, b, c)); ---- Turns Into: ---- this.someVar = someVar; this.a = a; this.b = b; this.c = c; ----What do you think about: class Foo { int a; float b; this( member a, member b ) { } } instead of: class Foo { int a; float b; this( int a, float b ) { this.a = a; this.b = b; } }I don't know that saving a few (very simple) lines of code is worth adding a new keyword and magic behaviour to ctor arguments. If you really wanted to just do that, you could probably write a mixin to take care of it.
Apr 26 2009
DisForDave wrote:downs Wrote:But that's the point - the language is powerful enough that the hacks work fine. No _need_ for a keyword.Nick Sabalausky wrote:but this is something the compiler should really be dealing with, it's such a common programming practice (the fact so many people have their own hacks for it is testament to this) i think it's worth a new keyword"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message news:gt159p$1bq0$1 digitalmars.com...I _also_ have something for this for Phobos (in tools.base), but it's intended for simple constructors. class Foo { int a; float b; mixin This!("a, b"); } Or to add some instruction: mixin This!("a; #b = 0f; "); Or to add a super call and defaults: mixin This!("a, super(b=0f)");Penguin wrote:I just happen to already have such a thing in my bag-o-tricks. See attachment. Usage: ---- mixin(initMember!(someVar)); mixin(initMember!(a, b, c)); ---- Turns Into: ---- this.someVar = someVar; this.a = a; this.b = b; this.c = c; ----What do you think about: class Foo { int a; float b; this( member a, member b ) { } } instead of: class Foo { int a; float b; this( int a, float b ) { this.a = a; this.b = b; } }I don't know that saving a few (very simple) lines of code is worth adding a new keyword and magic behaviour to ctor arguments. If you really wanted to just do that, you could probably write a mixin to take care of it.
Apr 26 2009
Do people really like such code and not cringe over how unclean and inelegant it is? Maybe it's just me...
Apr 26 2009
grauzone Wrote:Do people really like such code and not cringe over how unclean and inelegant it is? Maybe it's just me...it's not just you - i agree totally. If i wanted inelegant hacks, i'd be using C++ plus I always like a standard way of doing simple things.
Apr 26 2009
On Sun, 26 Apr 2009 04:17:04 -0400, Daniel Keep <daniel.keep.lists gmail.com> wrote:Penguin wrote:There are three benefits I see to the proposal: 1. It saves typing. This is not usually a big deal, except that this sort of constructor is EXTREMELY common with container objects. 2. It saves having to specify the type. We get the same savings from "auto," it should be similar here. In fact, you could probably change the keyword "member" to "auto". 3. It's a little more self documenting. You know when you see the function signature, that what you pass in will at least start being the initial value of the named member. The drawback of course is the keyword. the magic behavior is not a drawback IMO (in fact, it is the point). If you used auto as the keyword, you don't add a keyword. I'd vote for it. Of course, I'd rather have inherited constructors first ;) -SteveWhat do you think about: class Foo { int a; float b; this( member a, member b ) { } } instead of: class Foo { int a; float b; this( int a, float b ) { this.a = a; this.b = b; } }I don't know that saving a few (very simple) lines of code is worth adding a new keyword and magic behaviour to ctor arguments. If you really wanted to just do that, you could probably write a mixin to take care of it.
Apr 27 2009
Steven Schveighoffer wrote:... There are three benefits I see to the proposal: 1. It saves typing. This is not usually a big deal, except that this sort of constructor is EXTREMELY common with container objects.Fair enough.2. It saves having to specify the type. We get the same savings from "auto," it should be similar here. In fact, you could probably change the keyword "member" to "auto".True, but...3. It's a little more self documenting. You know when you see the function signature, that what you pass in will at least start being the initial value of the named member.class Act { this(auto a, auto b) {} // a million billion lines of code private { int a; Wombat b; } // another million billion lines of code. And a pony. } Working out the type of a or b will be a major pain in the arse. It's less documenting in the sense that you'll see the ctor and have no idea what you should be passing to it.The drawback of course is the keyword. the magic behavior is not a drawback IMO (in fact, it is the point).The magic-ness is a Bad Thing™. You're obfuscating the interface. This isn't like auto in code since there you care about how you use the thing, not the exact type. But here, you have users who NEED to know what the type is.If you used auto as the keyword, you don't add a keyword. I'd vote for it. Of course, I'd rather have inherited constructors first ;) -SteveFor me, the real nail-in-the-coffin is that you can do this with mixins. Yes, grauzone, they're not pretty. But they work and they don't require language changes. Besides, it gives us fuel to bug Walter about implementing macros. :D -- Daniel
Apr 27 2009
On Mon, 27 Apr 2009 08:51:03 -0400, Daniel Keep <daniel.keep.lists gmail.com> wrote:Steven Schveighoffer wrote:Just like: void foo() { int a, b; // a million billion lines of code auto c = a; // what type is a? }... There are three benefits I see to the proposal: 1. It saves typing. This is not usually a big deal, except that this sort of constructor is EXTREMELY common with container objects.Fair enough.2. It saves having to specify the type. We get the same savings from "auto," it should be similar here. In fact, you could probably change the keyword "member" to "auto".True, but...3. It's a little more self documenting. You know when you see the function signature, that what you pass in will at least start being the initial value of the named member.class Act { this(auto a, auto b) {} // a million billion lines of code private { int a; Wombat b; } // another million billion lines of code. And a pony. } Working out the type of a or b will be a major pain in the arse.Yes, the mixin Nick provided is handy. I guess you are right. -SteveIf you used auto as the keyword, you don't add a keyword. I'd vote for it. Of course, I'd rather have inherited constructors first ;) -SteveFor me, the real nail-in-the-coffin is that you can do this with mixins. Yes, grauzone, they're not pretty. But they work and they don't require language changes.
Apr 27 2009
"Penguin" <davidcollien gmail.com> >class Foo { int a; float b; this( member a, member b ) { } }Why the keyword ? Why not just: this(a,b) { } ?
Apr 26 2009
carlos smith:Why the keyword ? Why not just: this(a,b) { } ?Please, lets not mud the language too much for a small syntax gain. (What may be added to D is a syntax to create getters/setters in a simple and for this). Bye, bearophile
Apr 26 2009
On Sun, Apr 26, 2009 at 9:02 AM, bearophile <bearophileHUGS lycos.com> wrot= e:carlos smith:*snort* Wait a second, who's the one saying this?Why the keyword ? Why not just: =A0 this(a,b) { =A0} ?Please, lets not mud the language too much for a small syntax gain.
Apr 26 2009