| Thread overview | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
April 26, 2009 member arguments in D? | ||||
|---|---|---|---|---|
| ||||
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 Re: member arguments in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Penguin |
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 Re: member arguments in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Daniel Keep Attachments: | "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 Re: member arguments in D? (solution for Phobos | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | 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 Re: member arguments in D? (solution for Phobos | ||||
|---|---|---|---|---|
| ||||
Posted in reply to downs | 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 Re: member arguments in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Penguin | "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 Re: member arguments in D? (solution for Phobos | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DisForDave | 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 Re: member arguments in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to carlos smith | 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 Re: member arguments in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Nick Sabalausky | Do people really like such code and not cringe over how unclean and inelegant it is? Maybe it's just me... | |||
April 27, 2009 Re: member arguments in D? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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?
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply