Jump to page: 1 2
Thread overview
member arguments in D?
Apr 26, 2009
Penguin
Apr 26, 2009
Daniel Keep
Apr 26, 2009
Nick Sabalausky
Re: member arguments in D? (solution for Phobos
Apr 26, 2009
downs
Apr 26, 2009
DisForDave
Apr 26, 2009
downs
Apr 26, 2009
grauzone
Apr 27, 2009
DisForDave
Apr 27, 2009
Daniel Keep
Apr 26, 2009
carlos smith
Apr 26, 2009
bearophile
April 26, 2009
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;
   }

}
April 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
April 26, 2009
"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;
----



April 26, 2009
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)");
April 26, 2009
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
April 26, 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) {  } ?




April 26, 2009
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.
April 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 short way. We have discussed this in the past, C#4 shows a good starting point for this).

Bye,
bearophile
April 26, 2009
Do people really like such code and not cringe over how unclean and inelegant it is? Maybe it's just me...
April 27, 2009
On Sun, Apr 26, 2009 at 9:02 AM, bearophile <bearophileHUGS@lycos.com> wrote:
> carlos smith:
>> Why the keyword ?
>> Why not just:   this(a,b) {  } ?
>
> Please, lets not mud the language too much for a small syntax gain.

*snort*

Wait a second, who's the one saying this?
« First   ‹ Prev
1 2