Feature request - type inference for new

Steven Schveighoffer schveiguy at yahoo.com
Mon Jun 16 10:53:43 PDT 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;
>        }
>    }
>
> 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 





More information about the Digitalmars-d mailing list