On Fri, 28 Jun 2013 18:00:54 -0400, JS <[email protected]> wrote:
On Friday, 28 June 2013 at 14:02:07 UTC, Steven Schveighoffer wrote:
On Fri, 28 Jun 2013 02:51:39 -0400, JS <[email protected]> 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