Automatic typing

Ali Çehreli acehreli at yahoo.com
Mon Jul 1 20:17:58 PDT 2013


On 07/01/2013 07:35 PM, JS wrote:

 > On Tuesday, 2 July 2013 at 02:15:09 UTC, Andrei Alexandrescu wrote:

 >> auto a = 2.5; // fine, a is double
 >> ...
 >> a = 3;
 >>
 >
 > No, not under what I am talking about. You can't downgrade a type, only
 > upgrade it. a = 3, a is still a float. Using the concept I am talking
 > about, your example does nothing new.
 >
 > but reverse the numbers:
 >
 > auto a = 3;
 > a = 2.5;
 >
 > and a is now a float, and your logic then becomes correct EXCEPT a is
 > expanded, which is safe.
 >
 > I really don't know how to make it any clearer but I'm not sure if
 > anyone understands what I'm talking about ;/

I think I understand. I think I heard either on this or your other 
thread that function overloading may produce confusing results.

Consider the following program:

void foo(int i)
{}

void foo(double d)
{}

void main()
{
     auto a = 3;
     foo(a);

     // Some time later somebody adds the following line:
     a = 2.5;
}

If the type of 'a' would suddenly be double from that point on, foo(a) 
would silently go to a different function. It may be that calling the 
'double' overload is the right thing to do but it may as well be that it 
would be the completely the wrong thing to do.

The difference is, today the compiler warns me about the incompatible 
types. With the proposed feature, the semantics of the program might be 
different without any warning.

Of course one may argue that every line must be added very carefully and 
the unit tests must be comprehensive, etc. Of course I agree but I am 
another person who does not see the benefit of this proposal. It is 
never a chore to modify the type of a variable when the compiler warns 
me about an incompatibility.

Ali



More information about the Digitalmars-d mailing list