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

Walter Bright newshound2 at digitalmars.com
Fri May 20 03:27:12 UTC 2022


On 5/19/2022 5:06 PM, Steven Schveighoffer wrote:
> I'd happily write that in 
> exchange for not having this happen:
> 
> ```d
> enum A : int { a }
> 
> Json j = A.a;
> writeln(j); // false
> ```

I presume Json is a bool. And the bool is written as false. If it's bad that 0 
implicitly converts to a bool, then it should also be bad that 0 implicitly 
converts to char, ubyte, byte, int, float, etc. It implies all implicit 
conversions should be removed. While that is a reasonable point of view, I used 
a language that did that (Wirth's Pascal) and found it annoying and unpleasant.


>> And, as I remarked before, GPUs favor this style of coding, as does SIMD code, 
>> as does cryto code.
> 
> If the optimizer can't see through the ternary expression with 2 constants, then 
> maybe it needs updating.

To make the examples understandable, I use trivial cases.


> I want to write the clearest code possible, and let the optimizer wizards do 
> their magic.

I appreciate you want to write clear code. I do, too. The form I wrote is 
perfectly clear. Maybe it's just me, but I've never had any difficulty with the 
equivalence of:

   true, 1, +5V, On, Yes, T, +10V, etc.

I doubt that this gives anyone trouble, either:

enum Flags { A = 1, B = 2, C = 4, }

int flags = A | C;
if (flags & C) ...

It's clear to me that there is no set of rules that will please everyone and is 
objectively better than the others. At some point it ceases to be useful to 
continue to debate it, as no resolution will satisfy everyone.


More information about the Digitalmars-d mailing list