Should you be able to initialize a float with a char?

Paul Backus snarwin at gmail.com
Fri May 20 23:55:49 UTC 2022


On Friday, 20 May 2022 at 18:41:39 UTC, deadalnix wrote:
> On Friday, 20 May 2022 at 17:15:07 UTC, Paul Backus wrote:
>> In this example, both `int` and `bool` are implicit 
>> conversions, because the type of `E.a` is `E`, not `int`. So 
>> partial ordering is used to disambiguate, and the compiler 
>> (correctly) determines that the `bool` overload is more 
>> specialized than the `int` overload, because you can pass a 
>> `bool` argument to an `int` parameter but not the other way 
>> around.
>>
>> As soon as you allow the `E` -> `bool` implicit conversion 
>> (via VRP), everything else follows.
>
> Fair enough, because of the enum. You probably don't want to 
> cast do bool via VRP.
>
> But it also happens with integer literals, so clearly there is 
> a problem.

It happens with literals only if the literal type is not an exact 
match for the parameter type:

```d
import std.stdio;

void fun(int) { writeln("int"); }
void fun(bool) { writeln("bool"); }

void main()
{
     fun(int(0)); // int (exact match)
     fun(ubyte(0)); // bool (implicit conversion)
}
```

So, this case is exactly the same as the enum case. Once you 
allow the implicit conversion to `bool`, everything else follows 
from the normal language rules.


More information about the Digitalmars-d mailing list