www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - 'auto' and type inference

reply Mike Capp <mike.capp gmail.com> writes:
Just taking one of my occasional drive-by peeks to see where D is up to and came
across the following tidbit linked from the changelog. From
http://www.digitalmars.com/d/declaration.html :

"An implicit type inference for a class reference is not an auto declaration,
even if the auto storage class is used:"

Huh? Documented or no, this appears to violate the Principle Of Least
Astonishment with a wire brush, and looks like a straightforward bug to the
casual observer. Can anyone remember the rationale for this?

cheers
Mike
Nov 26 2005
parent reply John Reimer <terminal.node gmail.com> writes:
Mike Capp wrote:
 Just taking one of my occasional drive-by peeks to see where D is up to and
came
 across the following tidbit linked from the changelog. From
 http://www.digitalmars.com/d/declaration.html :
 
 "An implicit type inference for a class reference is not an auto declaration,
 even if the auto storage class is used:"
 
 Huh? Documented or no, this appears to violate the Principle Of Least
 Astonishment with a wire brush, and looks like a straightforward bug to the
 casual observer. Can anyone remember the rationale for this?
 
 cheers
 Mike
 
 
Have you seen the new context of "auto" recently added to the language? It's completely about making the compiler do type inference and has a very different meaning/application from the original. The old meaning of "auto" as "automatically destruct at end of scope" doesn't apply in those situations. The above is actually difficult and confusing to read if you are still thinking of auto its the original form (such as might be the case if you just popped in to see how things are going after being away for awhile). It's just saying that "new context" auto will never have a "old context" auto meaning if declared in the style shown in the first example (auto c = new C();) If you declare it like (auto C d = new C();), then the "old context" auto applies. Confusing? Yes, but I think the old context is going to be deprecated sometime. Not sure, though. -JJR
Nov 26 2005
parent reply "Garett Bass" <garettbass studiotekne.com> writes:
/* how about... */
{
    auto auto c = new C();
}
/* is c.~this() called? */

Garett 
Dec 02 2005
parent "Garett Bass" <garettbass studiotekne.com> writes:
Well, knowing is half the battle.  The difference between auto Foo foo and 
auto foo could be hard to spot.  An 'implicit' keyword would do the trick 
nicely, and prevent the auto/auto confusion.

-------------------------

private import std.stdio;

class Foo {
    char[] s;
    this(char[] s) {
        // Runtime Error: std.format
        //writefln("%s.this();", s);
        writef(s); writefln(".this();");
        this.s = s;
    }
    ~this() {
        // Runtime Error: std.format
        //writefln("%s.~this();", s);
        writef(s); writefln(".~this();");
    }
}

int main(char[][] args) {

    {
        auto Foo foo = new Foo("auto Foo foo");
    }

    writefln("auto Foo foo should have destructed by now.\n");

    {
        // compiler error: redundant storage class 'auto'
        //auto auto foo = new Foo("auto auto foo");
    }

    {
        auto foo = new Foo("auto foo");
    }

    writefln("auto foo has not destructed yet.");

    return 0;
}

/*
    auto Foo foo.this();
    auto Foo foo.~this();
    auto Foo foo should have destructed by now.

    auto foo.this();
    auto foo has not destructed yet.
    auto foo.~this();
*/


"Garett Bass" <garettbass studiotekne.com> wrote in message 
news:dmpdno$10uj$1 digitaldaemon.com...
 /* how about... */
 {
    auto auto c = new C();
 }
 /* is c.~this() called? */

 Garett
 
Dec 02 2005