January 18, 2018
On Wednesday, 17 January 2018 at 22:30:11 UTC, rumbu wrote:
> code like "m = n < 0 ? -n : n" doesn't worth a wrapper

That code is worth a wrapper, it's called "abs"...

m = abs(n);


January 18, 2018
On Thursday, 18 January 2018 at 02:30:17 UTC, Rubn wrote:
> On Wednesday, 17 January 2018 at 22:30:11 UTC, rumbu wrote:
>> code like "m = n < 0 ? -n : n" doesn't worth a wrapper
>
> That code is worth a wrapper, it's called "abs"...
>
> m = abs(n);

Well, since I'm in the learn forum and you seem to have a response to anything, can you help me translate this line under the new integer promotion rules?

https://github.com/rumbu13/decimal/blob/master/src/decimal/decimal.d#L7804

Thanks.
January 18, 2018
On Thursday, 18 January 2018 at 06:05:08 UTC, rumbu wrote:
> On Thursday, 18 January 2018 at 02:30:17 UTC, Rubn wrote:
>> On Wednesday, 17 January 2018 at 22:30:11 UTC, rumbu wrote:
>>> code like "m = n < 0 ? -n : n" doesn't worth a wrapper
>>
>> That code is worth a wrapper, it's called "abs"...
>>
>> m = abs(n);
>
> Well, since I'm in the learn forum and you seem to have a response to anything, can you help me translate this line under the new integer promotion rules?
>
> https://github.com/rumbu13/decimal/blob/master/src/decimal/decimal.d#L7804
>
> Thanks.

target = isNegative ? cast(Unsigned!T)(-c) : cast(Unsigned!T)c;

That would have been better even before the change, because the operator '-' used on unsigned types is likely to produce unexpected results, if the behaviour is defined at all.
January 18, 2018
On 1/17/18 2:40 PM, rumbu wrote:
> This started in the last DMD version (2.078):
> 
> byte b = -10;
> ulong u = b < 0 ? -b : b;
> 
> //Deprecation: integral promotion not done for `-b`, use '-transition=intpromote' switch or `-cast(int)(b)
> 
> Why do I need a to promote a byte to int to obtain an ulong? Even in the extreme case where b is byte.min, -byte.min as unsigned is exactly what i need: 128;
> 
> This leads to more cases:
> 
> ubyte u = cast(ubyte)-b;
> //Deprecation: integral promotion not done for `-b`, use '-transition=intpromote' switch or `-cast(int)(b)`
> 
> Last time I checked, casting is somehow synonym with "I know what I'm doing", why do I need another cast to prove my sanity: ubyte u = cast(ubyte)-cast(int)b;

I was going to respond with a helpful guide on what to do here, but I either misunderstand the docs, or I don't agree with the transition requirements.

I thought that you could simply ignore this deprecation message if it doesn't affect you (i.e. you don't have the possibility of seeing the corner cases that are fixed), but looking at the changelog it says:

"Once deprecated this will become an error, and then the C-like behavior will become the default."

So this says, even if you aren't affected, you *still* have to add casts to avoid a future error? I thought it was going to be deprecated, and then after some number of versions it would just be switched to the new behavior.

Is there going to be a point where casts aren't needed? Otherwise, this is pretty ugly.

-Steve
January 18, 2018
On 01/18/2018 03:30 PM, Steven Schveighoffer wrote:
> Is there going to be a point where casts aren't needed? Otherwise, this is pretty ugly.

You don't need casts when you use `-transition=intpromote`.
January 18, 2018
On 1/18/18 11:14 AM, ag0aep6g wrote:
> On 01/18/2018 03:30 PM, Steven Schveighoffer wrote:
>> Is there going to be a point where casts aren't needed? Otherwise, this is pretty ugly.
> 
> You don't need casts when you use `-transition=intpromote`.

Sure, but what does the statement "Once deprecated this will become an error" mean? Will I have to use the -transition=intpromote switch forever to avoid an error?

Thinking about the fact that the deprecation doesn't show an error for the -transition switch, makes me think possibly the changelog message is incorrect.

-Steve
January 18, 2018
On 01/18/2018 05:22 PM, Steven Schveighoffer wrote:
> Sure, but what does the statement "Once deprecated this will become an error" mean? Will I have to use the -transition=intpromote switch forever to avoid an error?

As you quoted before: "Once deprecated this will become an error, and then the C-like behavior will become the default."

I'm interpreting that to mean that it will become an error for some time, but later it will be allowed again with the new behavior. And then you can throw away `-transition=intpromote`.
January 18, 2018
On Thursday, 18 January 2018 at 12:51:48 UTC, Dominikus Dittes Scherkl wrote:
> On Thursday, 18 January 2018 at 06:05:08 UTC, rumbu wrote:
>> On Thursday, 18 January 2018 at 02:30:17 UTC, Rubn wrote:
>>> On Wednesday, 17 January 2018 at 22:30:11 UTC, rumbu wrote:
>>>> code like "m = n < 0 ? -n : n" doesn't worth a wrapper
>>>
>>> That code is worth a wrapper, it's called "abs"...
>>>
>>> m = abs(n);
>>
>> Well, since I'm in the learn forum and you seem to have a response to anything, can you help me translate this line under the new integer promotion rules?
>>
>> https://github.com/rumbu13/decimal/blob/master/src/decimal/decimal.d#L7804
>>
>> Thanks.
>
> target = isNegative ? cast(Unsigned!T)(-c) : cast(Unsigned!T)c;
>
> That would have been better even before the change, because the operator '-' used on unsigned types is likely to produce unexpected results, if the behaviour is defined at all.

I don't think so:

ulong c = 128;
bool isNegative = true;
byte target = isNegative ? -cast(ubyte)c : cast(ubyte)c;

Error Deprecation: integral promotion not done for `-cast(ubyte)c`, use '-transition=intpromote' switch or `-cast(int)(cast(ubyte)c)`		
January 18, 2018
On Thursday, 18 January 2018 at 17:54:59 UTC, rumbu wrote:
> On Thursday, 18 January 2018 at 12:51:48 UTC, Dominikus Dittes

>>
>> target = isNegative ? cast(Unsigned!T)(-c) : cast(Unsigned!T)c;
>>
>> That would have been better even before the change, because the operator '-' used on unsigned types is likely to produce unexpected results, if the behaviour is defined at all.
>
> I don't think so:
>
> ulong c = 128;
> bool isNegative = true;
> byte target = isNegative ? -cast(ubyte)c : cast(ubyte)c;
>
> Error Deprecation: integral promotion not done for `-cast(ubyte)c`, use '-transition=intpromote' switch or `-cast(int)(cast(ubyte)c)`		

My bad, it works. Thanks:

ulong c = 128;
bool isNegative = true;
byte target = isNegative ? cast(ubyte)-c : cast(ubyte)c;
January 18, 2018
On Thursday, 18 January 2018 at 18:00:51 UTC, rumbu wrote:
> On Thursday, 18 January 2018 at 17:54:59 UTC, rumbu wrote:
>> On Thursday, 18 January 2018 at 12:51:48 UTC, Dominikus Dittes
>
>>>
>>> target = isNegative ? cast(Unsigned!T)(-c) : cast(Unsigned!T)c;
>>>
>>> That would have been better even before the change, because the operator '-' used on unsigned types is likely to produce unexpected results, if the behaviour is defined at all.
>>
>> I don't think so:
>>
>> ulong c = 128;
>> bool isNegative = true;
>> byte target = isNegative ? -cast(ubyte)c : cast(ubyte)c;
>>
>> Error Deprecation: integral promotion not done for `-cast(ubyte)c`, use '-transition=intpromote' switch or `-cast(int)(cast(ubyte)c)`		
>
> My bad, it works. Thanks:
>
> ulong c = 128;
> bool isNegative = true;
> byte target = isNegative ? cast(ubyte)-c : cast(ubyte)c;

But this doesn't:

ushort c = 128;
bool isNegative = true;
byte target = isNegative ? cast(ubyte)-c : cast(ubyte)c;

Error Deprecation: integral promotion not done for `-c`, use '-transition=intpromote' switch or `-cast(int)(c)

This is starting to become truly crazy.