www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature request - type inference for new

reply "Janice Caron" <caron800 googlemail.com> writes:
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
next sibling parent "Koroskin Denis" <2korden gmail.com> writes:
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
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"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
Jun 16 2008
parent "Janice Caron" <caron800 googlemail.com> writes:
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
prev sibling next sibling parent reply Jason House <jason.james.house gmail.com> writes:
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
next sibling parent "Nick Sabalausky" <a a.a> writes:
"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
prev sibling parent reply Yossarian <xtauer01 stud.fit.vutbr.cz> writes:
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
parent BCS <ao pathlink.com> writes:
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;
Jun 17 2008
prev sibling parent downs <default_357-line yahoo.de> writes:
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
Jun 17 2008