www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - member arguments in D?

reply Penguin <davidcollien gmail.com> writes:
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
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
next sibling parent reply "Nick Sabalausky" <a a.a> writes:
"Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
news:gt159p$1bq0$1 digitalmars.com...
 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.
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 ` end
Apr 26 2009
next sibling parent reply downs <default_357-line yahoo.de> writes:
Nick Sabalausky wrote:
 "Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
 news:gt159p$1bq0$1 digitalmars.com...
 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.
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; ----
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)");
Apr 26 2009
parent reply DisForDave <dave icy.com.au> writes:
downs Wrote:

 Nick Sabalausky wrote:
 "Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
 news:gt159p$1bq0$1 digitalmars.com...
 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.
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; ----
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)");
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
Apr 26 2009
parent downs <default_357-line yahoo.de> writes:
DisForDave wrote:
 downs Wrote:
 
 Nick Sabalausky wrote:
 "Daniel Keep" <daniel.keep.lists gmail.com> wrote in message 
 news:gt159p$1bq0$1 digitalmars.com...
 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.
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; ----
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)");
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
But that's the point - the language is powerful enough that the hacks work fine. No _need_ for a keyword.
Apr 26 2009
prev sibling parent reply grauzone <none example.net> writes:
Do people really like such code and not cringe over how unclean and 
inelegant it is? Maybe it's just me...
Apr 26 2009
parent DisForDave <dave icy.com.au> writes:
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
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 26 Apr 2009 04:17:04 -0400, Daniel Keep  
<daniel.keep.lists gmail.com> wrote:

 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.
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 ;) -Steve
Apr 27 2009
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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 ;)
 
 -Steve
For 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
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 27 Apr 2009 08:51:03 -0400, Daniel Keep  
<daniel.keep.lists gmail.com> wrote:

 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.
Just like: void foo() { int a, b; // a million billion lines of code auto c = a; // what type is a? }
 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 ;)

 -Steve
For 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.
Yes, the mixin Nick provided is handy. I guess you are right. -Steve
Apr 27 2009
prev sibling parent reply "carlos smith" <carlos-smith sympatico.ca> writes:
"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
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sun, Apr 26, 2009 at 9:02 AM, bearophile <bearophileHUGS lycos.com> wrot=
e:
 carlos smith:
 Why the keyword ?
 Why not just: =A0 this(a,b) { =A0} ?
Please, lets not mud the language too much for a small syntax gain.
*snort* Wait a second, who's the one saying this?
Apr 26 2009