Annoyance with new integer promotion deprecations

Steven Schveighoffer schveiguy at yahoo.com
Tue Feb 6 00:08:12 UTC 2018


On 2/5/18 6:56 PM, Adam D. Ruppe wrote:
> On Monday, 5 February 2018 at 23:34:59 UTC, Steven Schveighoffer wrote:
>> But for integer division and assignment to float, it's quite obvious 
>> that it doesn't work with almost all combinations.
> 
> So let me take two steps there to get to my point:
> 
> 1) Given:
>      byte a, b;
>      byte c = a + b;
> 
> The cast seems a bit silly: you are already explicitly using `byte` 
> everywhere, so your intention is pretty clear: you only want to use the 
> bytes and are ok with the rest of it being discarded. Therefore, I find 
> the cast to be an unnecessary addition.

It could be done that way. In fact, this works in C:

char a = 1;
char b = 2;
char c = a + b;

I would actually have no problem if it were this way, as you are clear 
in your intention. I'm also OK with the way it is now, where it requires 
the cast. The cast is generally added "because the compiler told me to", 
but it does make you think about what you really want.

> 2) Change it to:
>      byte a, b;
>      int c = a + b;
> 
> This is directly analogous to the float example. Which, I agree, 
> reasonable people can disagree on... but I'd be perfectly ok if the `a + 
> b` was still typed `byte` just like above... and the data got discarded 
> when assigned to the `int`.

I think the CPU has to do extra work to throw away that high bit, no?

But actually, I would be OK with this as well, as long as it was 
consistent across all operations. Right now, it's one way for unary 
operations and another way for binary operations. Just pick a way and 
make it consistent.

I still don't love the idea that:

a = a + 1;

fails, but

++a; a += 1;

works just fine.

> But I will grant it is going to be different than the C rules... so it 
> would make copy/pasted code a pain. tho copy/pasted code is already a 
> pain cuz you gotta insert casts to avoid the errors... but yeah an error 
> is less of a hassle than silent change.
> 
> So I get why things are the way they are. I just still don't like it :P

Yeah, there's a lot of stuff that's like that ;)

-Steve


More information about the Digitalmars-d mailing list