On Friday, 28 June 2013 at 22:29:21 UTC, Walter Bright wrote:
On 6/27/2013 5:34 PM, JS wrote:
Would it be possible for a language(specifically d) to have the ability to automatically type a variable by looking at its use cases without adding too much complexity? It seems to me that most compilers already can infer type
mismatchs which would allow them to handle stuff like:

main()
{
   auto x;
   auto y;
   x = 3;   // x is an int, same as auto x = 3;
   y = f(); // y is the same type as what f() returns
x = 3.9; // x is really a float, no mismatch with previous type(int)
}

in this case x and y's type is inferred from future use. The compiler essentially just lazily infers the variable type. Obviously ambiguity will
generate an error.


I don't see a compelling use case for this proposal, or even any use case. There'd have to be some serious advantage to it to justify its complexity.

Is variant useful? If not then you have a point. I'm not proposing anything that variant can't already do except add compile time performance. I do not think the complexity is much more than what is already done.

D already checks for time mismatch. With such a variant or auto the check simply is more intelligent.

e.g.,

auto x;  // x's type is undefined or possibly variant.
x = 3;   // x's type is set temporarily to an int
...
x = 3.0; // ****


at **** we have several possibilities.

1. Throw an error, this makes auto more useful and avoids many pitfalls. 2. Set x's type to a variant. [possibly goto 3 if castable to new type]
   3. Set x's type to a double.

Both 2 and 3 require an extra pass to convert the code because it uses forward inference.

auto looks only at the immediate assignment expression to determine the type. I am talking about generalizing it to look in the scope with possible fallback to a variant type with optional warning, an error, or type enlargement.


Reply via email to