Make -preview=intpromote not suck
Steven Schveighoffer
schveiguy at gmail.com
Wed Oct 28 14:52:28 UTC 2020
On 10/27/20 7:13 PM, Dukc wrote:
> On Monday, 26 October 2020 at 13:06:09 UTC, Steven Schveighoffer wrote:
>> [snip]
>>
>
> Wouldn't it be simpler if integer promoting expressions were implicitly
> castable to types they were promoted from, meaning:
>
> ```
> short a = 1, b = 2;
> short c = a+b; //fine, int promoted from short implicitly castable back
> auto i = a+b; //int, because the type is still int if not casted
> short d = cast(short)i; //cast required here, promotion reversibility
> applies only for expressions
> byte e = cast(byte)(a+b); //cast required because byte is smaller than
> short
> ```
>
> Won't solve both your problem and a whole host of others with `byte`s
> and `short`s?
>
> It should not be too hard to implement either, as array literals already
> do the same. They are implicitly castable to static arrays of their own
> size, while their type (dynamic array) is not.
I don't know if this is OK or correct. For example
short c = a * b; => lots of truncation gonna happen.
Note that C allows this, and D does not, on purpose.
I wanted to focus on the problem that *currently* compiles, will *not*
compile after the change, and *behaves exactly the same* even with
mitigation (casting). In other words, we would require lots of mindless
busywork (and/or break lots of old code) for zero benefit.
Essentially, I like the idea of the intpromote preview. But this one
problem is going to derail its adoption.
-Steve
More information about the Digitalmars-d
mailing list