PROPOSAL: Implicit conversions of integer literals to floating

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Dec 30 12:34:45 PST 2010


On 12/30/10 2:13 PM, Don wrote:
> bearophile wrote:
>> Don:
>>
>>> No. People expect sqrt(2) to compile, and to return 1.414....
>>
>> Then people probably need to use sqrt(2.0) or sqrt(cast(double)2).
>
> I've got a lot of sympathy for the first alternative (absolutely NONE
> for the second!). But unfortunately, the language allows sqrt(2) to
> compile if there is only a single sqrt function.
> We have here a feature which works in some cases, but not in others.

I agree there's an issue here, but not only with floating point numbers:

void fun(long);
void fun(ulong);
...
fun(2); // whaa?

The same problem is at work here: 2 is an int and is equally inclined to 
go to either long or ulong.

Generally I find it a bit difficult to integrate this proposal within 
the conversions framework that we have; it adds an entire new concept 
into the mix. This makes me be more conservative. For literals, all 
things considered, I don't think requiring the .0 is a major hindrance. 
For non-literals that's more of an issue:

int x = 42;
sqrt(x); // whaa?

I'm not sure to what extent this is a problem; I defer that opinion to 
Don. If this is indeed an important issue, we should address it. If not, 
I think improving the situation for literals exclusively would provide 
only marginal benefit.


Andrei


More information about the Digitalmars-d mailing list