Annoyance with new integer promotion deprecations

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 5 22:52:41 UTC 2018


On 2/5/18 3:45 PM, H. S. Teoh wrote:
> On Mon, Feb 05, 2018 at 03:23:20PM -0500, Steven Schveighoffer via Digitalmars-d wrote:
>> On 2/5/18 2:30 PM, H. S. Teoh wrote:
>>> Even better yet:
>>>
>>> 	byte b;
>>> 	b = -b;		// Deprecation error
>>>
>>> WAT????
>>
>> In the future, -b will be typed as an int, so you can't reassign it to
>> b.  You can see this today with -transition=intpomote:
>>
>> Error: cannot implicitly convert expression -cast(int)b of type int to
>> byte
> [...]
> 
> Honestly, this just makes narrow int types basically useless when it
> comes to any arithmetic at all.

They already are:

b = b + 1; // error.

But I can't see why there is controversy over negation of byte turning 
into an int. I can't see why anyone would expect:

int x = -b;

when b is -128, to set x to -128. The integer promotion makes complete 
sense to me.

> Sticking to C promotion rules is one of the scourges that continue to
> plague D;

C promotion rules came along with the feature of assigning the result 
back to a byte. D got rid of that "convenience", and I think it was a 
good decision. I hardly ever finding myself regretting the compiler 
telling me that I'm about to throw away information.

> another example is char -> byte confusion no thanks to C
> traditions:
> 
> 	int f(dchar ch) { return 1; }
> 	int f(byte i) { return 2; }
> 	void main() {
> 		pragma(msg, f('a'));
> 		pragma(msg, f(1));
> 	}
> 
> Exercise for reader: guess compiler output.

char and byte should not ever mix, IMO. char and dchar should not ever 
mix either. There have been some nasty bugs in phobos due to the fact 
that char auto-promotes to dchar.

-Steve


More information about the Digitalmars-d mailing list