member arguments in D? (solution for Phobos

downs default_357-line at yahoo.de
Sun Apr 26 05:35:21 PDT 2009


DisForDave wrote:
> downs Wrote:
> 
>> Nick Sabalausky wrote:
>>> "Daniel Keep" <daniel.keep.lists at gmail.com> wrote in message 
>>> news:gt159p$1bq0$1 at 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.



More information about the Digitalmars-d mailing list