member arguments in D? (solution for Phobos

downs default_357-line at yahoo.de
Sun Apr 26 04:57:23 PDT 2009


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)");



More information about the Digitalmars-d mailing list