member arguments in D?

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 27 05:14:39 PDT 2009


On Sun, 26 Apr 2009 04:17:04 -0400, Daniel Keep  
<daniel.keep.lists at 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



More information about the Digitalmars-d mailing list