Automatic typing

Timon Gehr timon.gehr at gmx.ch
Fri Jun 28 16:42:20 PDT 2013


On 06/29/2013 12:29 AM, 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.

Eg:

auto a;
if(x in cache) a=cache[x];
else cache[x]=a=new AnnoyingToSpellOutBeforeTheIf!"!"();

Using the type of the lexically first assignment would often be good 
enough, where reading the variable is disallowed prior to this first 
assignment.

A little better (and still decidable) would be using the common type of 
all branches' first assignments not preceded by a read.


More information about the Digitalmars-d mailing list