Why there is too many uneccessary casts?

Steven Schveighoffer schveiguy at yahoo.com
Tue Jun 11 10:25:24 PDT 2013


On Tue, 11 Jun 2013 12:18:52 -0400, Adam D. Ruppe  
<destructionator at gmail.com> wrote:

> On Tuesday, 11 June 2013 at 16:05:30 UTC, Steven Schveighoffer wrote:
>> CPU performs math at int level.
>
> eh, I wouldn't blame the hardware. You can do
>
> asm {
>    mov AL, 10;
>    add AL, 5;
> }
>
> and it is allowed, it also don't spill into AH if you overflow it (it  
> just sets the carry flag). I'm sure it is different on different  
> processors, but x86 is pretty flexible.

Well, that is not what I knew, so good lesson :)

But I'd be surprised if the above was less costly than a full word  
addition.  I think that is the point of C's policy of integer promotion,  
which D adopts for the most part (although C allows it to go back into  
whatever).

>
>
>> ubyte c = k;
>> c += 1; // ok
>>
>> This I find extremely inconsistent...
>
> I'd be extremely annoyed if that required a cast. It's bleeding obvious  
> that you want it to assign back there....

I argue the consistency is both ways :)  I don't want to require a cast  
there (and in reality, where would the cast go?), but it seems that if you  
are doing math among bytes, or a byte and an integer that can fit into a  
byte, it should be quite obvious that you are working in the byte land.   
The fact that

k += 1;

and

k = k + 1;

are treated differently is annoying.

-Steve


More information about the Digitalmars-d-learn mailing list