Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 16, 2008 Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
I've been quiet for a while. (Actually, I've been offline for a while). So, I thought I'd better come back with a new feature request! :-) Type inference for new. Here's an example: class C(T) { Some!(Funky!(Class!(T))) funky; this() { funky = new; } } The type of funky is in the declaration, so why repeat it? Another example... class AnotherClassWithAVeryLongName { int x,y; this(int x, int y) { this.x = x; this.y = y; } } void foo(int a, int b) { AnotherClassWithAVeryLongName c; if (a < b) c = new(a,b); else c = new(b,a); } Again, no need to repeat the type of c, because it's in the declaration (this time a local declaration). This will only work for assignments, because D doesn't overload on return type. But still - I'm all for anything that saves typing. |
June 16, 2008 Re: Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | On Mon, 16 Jun 2008 21:23:19 +0400, Janice Caron <caron800@googlemail.com> wrote:
> I've been quiet for a while. (Actually, I've been offline for a
> while). So, I thought I'd better come back with a new feature request!
> :-) Type inference for new. Here's an example:
>
> class C(T)
> {
> Some!(Funky!(Class!(T))) funky;
>
> this()
> {
> funky = new;
> }
> }
>
> The type of funky is in the declaration, so why repeat it?
>
> Another example...
>
> class AnotherClassWithAVeryLongName
> {
> int x,y;
>
> this(int x, int y) { this.x = x; this.y = y; }
> }
>
> void foo(int a, int b)
> {
> AnotherClassWithAVeryLongName c;
>
> if (a < b) c = new(a,b);
> else c = new(b,a);
> }
>
> Again, no need to repeat the type of c, because it's in the
> declaration (this time a local declaration).
>
> This will only work for assignments, because D doesn't overload on
> return type. But still - I'm all for anything that saves typing.
Well, this one doesn't improve readability. I wouldn't use that, especially if there is a bulk of code between
AnotherClassWithAVeryLongName c;
and
c = new(a,b);
|
June 16, 2008 Re: Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | "Janice Caron" wrote > I've been quiet for a while. (Actually, I've been offline for a while). So, I thought I'd better come back with a new feature request! :-) Type inference for new. Here's an example: > > class C(T) > { > Some!(Funky!(Class!(T))) funky; > > this() > { > funky = new; > } > } > > The type of funky is in the declaration, so why repeat it? You mean like: funky = new typeof(funky); ??? Besides that, I would really REALLY like to have: class X { auto n = new SomeOtherClass; } be equivalent to putting n = new SomeOtherClass in the front of all the constructors. > > Another example... > > class AnotherClassWithAVeryLongName > { > int x,y; > > this(int x, int y) { this.x = x; this.y = y; } > } > > void foo(int a, int b) > { > AnotherClassWithAVeryLongName c; > > if (a < b) c = new(a,b); > else c = new(b,a); > } > > Again, no need to repeat the type of c, because it's in the declaration (this time a local declaration). > > This will only work for assignments, because D doesn't overload on return type. But still - I'm all for anything that saves typing. if (a < b) c = new typeof(c)(a, b); else c = new typeof(c)(b, a); -Steve |
June 16, 2008 Re: Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | I'd prefer using the auto keyword as a replacement for the missing type... ie. the following candidate syntaxes:
funky = new auto;
funky = new auto();
c = new auto(a,b);
c = new auto(b,a);
Janice Caron Wrote:
> I've been quiet for a while. (Actually, I've been offline for a while). So, I thought I'd better come back with a new feature request! :-) Type inference for new. Here's an example:
>
> class C(T)
> {
> Some!(Funky!(Class!(T))) funky;
>
> this()
> {
> funky = new;
> }
> }
>
> The type of funky is in the declaration, so why repeat it?
>
> Another example...
>
> class AnotherClassWithAVeryLongName
> {
> int x,y;
>
> this(int x, int y) { this.x = x; this.y = y; }
> }
>
> void foo(int a, int b)
> {
> AnotherClassWithAVeryLongName c;
>
> if (a < b) c = new(a,b);
> else c = new(b,a);
> }
>
> Again, no need to repeat the type of c, because it's in the declaration (this time a local declaration).
>
> This will only work for assignments, because D doesn't overload on return type. But still - I'm all for anything that saves typing.
|
June 16, 2008 Re: Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 16/06/2008, Steven Schveighoffer <schveiguy@yahoo.com> wrote:
> if (a < b) c = new typeof(c)(a, b);
> else c = new typeof(c)(b, a);
That's clever. I didn't think of that.
OK - request withdrawn.
|
June 16, 2008 Re: Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jason House | "Jason House" <jason.james.house@gmail.com> wrote in message news:g36f76$m7u$1@digitalmars.com... > I'd prefer using the auto keyword as a replacement for the missing type... > ie. the following candidate syntaxes: > funky = new auto; > funky = new auto(); > c = new auto(a,b); > c = new auto(b,a); > I'd have to agree with this. "c = new typeof(c);" is an improvement over repeating the entire type, but still isn't totally DRY. Before you posted I was kinda thinking something like "c = new typeof(_lvalue);", but "c = new auto();" is much cleaner. |
June 17, 2008 Re: Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jason House | Dne Mon, 16 Jun 2008 21:33:58 +0200 Jason House <jason.james.house@gmail.com> napsal/-a: > I'd prefer using the auto keyword as a replacement for the missing type... ie. the following candidate syntaxes: > funky = new auto; > funky = new auto(); > c = new auto(a,b); > c = new auto(b,a); > I'm not sure, if this suggestion doesn't break the language context-less grammar. imagine: auto c = new auto (); .. what now? This is nonsense, but gramatically correct. > Janice Caron Wrote: > >> I've been quiet for a while. (Actually, I've been offline for a >> while). So, I thought I'd better come back with a new feature request! >> :-) Type inference for new. Here's an example: >> >> class C(T) >> { >> Some!(Funky!(Class!(T))) funky; >> >> this() >> { >> funky = new; >> } >> } >> >> The type of funky is in the declaration, so why repeat it? >> >> Another example... >> >> class AnotherClassWithAVeryLongName >> { >> int x,y; >> >> this(int x, int y) { this.x = x; this.y = y; } >> } >> >> void foo(int a, int b) >> { >> AnotherClassWithAVeryLongName c; >> >> if (a < b) c = new(a,b); >> else c = new(b,a); >> } >> >> Again, no need to repeat the type of c, because it's in the >> declaration (this time a local declaration). >> >> This will only work for assignments, because D doesn't overload on >> return type. But still - I'm all for anything that saves typing. > -- Tato zpráva byla vytvořena převratným poštovním klientem Opery: http://www.opera.com/mail/ |
June 17, 2008 Scrapple.Tools to the rescue! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | Janice Caron wrote: > I've been quiet for a while. (Actually, I've been offline for a while). So, I thought I'd better come back with a new feature request! :-) Type inference for new. Here's an example: > > class C(T) > { > Some!(Funky!(Class!(T))) funky; > > this() > { > funky = new; > } > } > I quote from tools.base: > void New(S, T...)(ref S inst, T t) { inst=new S(t); } Example usage: > this() > { > New(funky); > } --downs |
June 17, 2008 Re: Feature request - type inference for new | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yossarian | Reply to Yossarian,
> Dne Mon, 16 Jun 2008 21:33:58 +0200 Jason House
> <jason.james.house@gmail.com> napsal/-a:
>
>> I'd prefer using the auto keyword as a replacement for the missing
>> type... ie. the following candidate syntaxes:
>> funky = new auto;
>> funky = new auto();
>> c = new auto(a,b);
>> c = new auto(b,a);
> I'm not sure, if this suggestion doesn't break the language
> context-less
> grammar.
> imagine:
> auto c = new auto ();
>
> .. what now? This is nonsense, but gramatically correct.
>
That's fine, the syntax doesn't catch everything.
this is valid syntax right now but doesn't pass the semantic checks:
int a = 1;
char[] b = "abc"
auto c = a ~ b;
|
Copyright © 1999-2021 by the D Language Foundation