digitalmars.D - Feature request - type inference for new
- "Janice Caron" <caron800 googlemail.com> Jun 16 2008
- "Koroskin Denis" <2korden gmail.com> Jun 16 2008
- "Steven Schveighoffer" <schveiguy yahoo.com> Jun 16 2008
- Jason House <jason.james.house gmail.com> Jun 16 2008
- "Nick Sabalausky" <a a.a> Jun 16 2008
- "Janice Caron" <caron800 googlemail.com> Jun 16 2008
- Yossarian <xtauer01 stud.fit.vutbr.cz> Jun 16 2008
- BCS <ao pathlink.com> Jun 17 2008
- downs <default_357-line yahoo.de> Jun 17 2008
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.
Jun 16 2008
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);
Jun 16 2008
"Janice Caron" wroteI'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
Jun 16 2008
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.
Jun 16 2008
"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.
Jun 16 2008
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.
Jun 16 2008
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/
Jun 16 2008
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);
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;
Jun 17 2008
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; } }
void New(S, T...)(ref S inst, T t) { inst=new S(t); }
Example usage:this() { New(funky); }
--downs
Jun 17 2008









"Koroskin Denis" <2korden gmail.com> 