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

max haughton maxhaton at gmail.com
Thu May 19 15:37:25 UTC 2022


On Thursday, 19 May 2022 at 14:33:14 UTC, Steven Schveighoffer 
wrote:
> On 5/19/22 12:35 AM, Walter Bright wrote:
>> On 5/18/2022 5:47 PM, Steven Schveighoffer wrote:
>>> But I have little hope for it, as Walter treats a boolean as 
>>> an integer.
>> 
>> They *are* integers.
>> 
>> The APL language relies on bools being integers so conditional 
>> operations can be carried out without branching :-)
>> 
>> I.e.
>>      a = (b < c) ? 8 : 3;
>> 
>> becomes:
>> 
>>      a = 3 + (b < c) * 5;   // I know this is not APL syntax
>> 
>> That works in D, too!
>
> I hope we are not depending on the type system to the degree 
> where a bool must be an integer in order to have this kind of 
> optimization.
>
> -Steve

That and you can have the underlying type without exposing it to 
the programmer. "Bools are integers", as opposed to bools not 
having a memory representation at all?

Basically any discussion of these peephole optimizations (if this 
is more than just a nice to have) is a bit silly in the age where 
GCC and LLVM will both reach this kind of code anywhere because 
they want to eliminate branches like the plague (even if they 
couldn't do it in the first place given that you'd need to tell 
it what a bool is)



More information about the Digitalmars-d mailing list