Automatic typing

Steven Schveighoffer schveiguy at yahoo.com
Fri Jun 28 20:39:29 PDT 2013


On Fri, 28 Jun 2013 18:00:54 -0400, JS <js.mdnq at gmail.com> wrote:

> On Friday, 28 June 2013 at 14:02:07 UTC, Steven Schveighoffer wrote:
>> On Fri, 28 Jun 2013 02:51:39 -0400, JS <js.mdnq at gmail.com> My argument  
>> is that auto should be left the way it is.  I don't want it to change.   
>> And variant already does what you want with less confusing semantics,  
>> no reason to add another feature.
>>
>> -Steve
>
> Using the auto keyword was just an example.  My argument does not depend  
> the specific keyword used. My "idea" is simply generalizing auto to use  
> forward inferencing.
>
> variant is NOT what I am talking about. It is not a performant time but  
> a union of types. I am talking about the compiler finding the best  
> choice for the type by looking ahead of the definition of the time.

There is a possible way to solve this -- auto return types.  If you can  
fit your initialization of the variable into a function (even an inner  
function) that returns auto, then the compiler should be able to figure  
out the best type.

Example:

import std.stdio;

void main(string[] args)
{
     auto foo() {
         if(args.length > 1 && args[1] == "1")
             return 1;
         else
             return 2.5;
     }
     auto x = foo();
     writeln(typeof(x).stringof);
}

this outputs "double".

Granted, it doesn't solve the "general" case, where you want to use x  
before it's initialized a second time, but I think that really is just a  
programming error -- use a different variable.

-Steve


More information about the Digitalmars-d mailing list