www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - short s, t; t = -s: no (longer) works: Deprecation: integral

reply kdevel <kdevel vogtner.de> writes:
I don't get the point of the deprecation message:

--- intprom.d
import std.stdio;

void main ()
{
    short s, t;
    t = -s;
}
---

$ dmd intprom.d
intprom.d(6): Deprecation: integral promotion not done for -s, 
use '-transition=intpromote' switch or -cast(int)(s)

What shall I do in order to get my template code

void mymain (T) ()
{
    :
       b[i] = -b [i];
    :
}

compiled for any type for which negation is defined?
Feb 24 2018
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/24/18 3:07 PM, kdevel wrote:
 I don't get the point of the deprecation message:
 
 --- intprom.d
 import std.stdio;
 
 void main ()
 {
     short s, t;
     t = -s;
 }
 ---
https://dlang.org/changelog/2.078.0.html#fix16997
 
 $ dmd intprom.d
 intprom.d(6): Deprecation: integral promotion not done for -s, use 
 '-transition=intpromote' switch or -cast(int)(s)
 
 What shall I do in order to get my template code
 
 void mymain (T) ()
 {
     :
        b[i] = -b [i];
     :
 }
 
 compiled for any type for which negation is defined?
b[i] = cast(typeof(b[i]))-b[i]; And then use -transition=intpromote. Note, your function wasn't real code, so maybe if you have the type of b[i] somewhere it might look better than what I wrote (like maybe cast(T)-b[i]). -Steve
Feb 24 2018
parent reply kdevel <kdevel vogtner.de> writes:
On Saturday, 24 February 2018 at 20:17:12 UTC, Steven 
Schveighoffer wrote:
 On 2/24/18 3:07 PM, kdevel wrote:
 I don't get the point of the deprecation message:
 
 --- intprom.d
 import std.stdio;
 
 void main ()
 {
     short s, t;
     t = -s;
 }
 ---
https://dlang.org/changelog/2.078.0.html#fix16997
My goodness! So there is currently no negation operator defined on short and some other types?
 $ dmd intprom.d
 intprom.d(6): Deprecation: integral promotion not done for -s, 
 use '-transition=intpromote' switch or -cast(int)(s)
 
 What shall I do in order to get my template code
 
 void mymain (T) ()
 {
     :
        b[i] = -b [i];
     :
 }
 
 compiled for any type for which negation is defined?
b[i] = cast(typeof(b[i]))-b[i]; And then use -transition=intpromote. Note, your function wasn't real code, so maybe if you have the type of b[i] somewhere it might look better than what I wrote (like maybe cast(T)-b[i]).
Any objections against leaving out the compiler switch and using b[i] = cast (T) (0 - b[i]); instead?
Feb 24 2018
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/24/18 4:42 PM, kdevel wrote:
 On Saturday, 24 February 2018 at 20:17:12 UTC, Steven Schveighoffer wrote:
 https://dlang.org/changelog/2.078.0.html#fix16997
My goodness! So there is currently no negation operator defined on short and some other types?
No, that's not the case. It's simply defined incorrectly. The prime example is this: byte b = -128; int x = -b; What would you expect x to be? a) 128 b) -128 Currently, the answer is b. In C, the answer is a. With the -transition=intpromote switch, the answer is changed to a. The reason it's so annoying is because we can't break code without first warning about it. This will change behavior in some cases. But chances are in most cases, you really wanted what C did, or your code would never hit the corner cases anyway (byte.min and short.min are so rare in the wild). Eventually, the intpromote switch will go away, and a will be the permanent answer.
 Any objections against leaving out the compiler switch and using
 
     b[i] = cast (T) (0 - b[i]);
 
 instead?
You can do that too, seems like a good workaround. The current requirement that you first have to cast to int, and then cast back, is a bit over the top. See here: https://issues.dlang.org/show_bug.cgi?id=18380 -Steve
Feb 24 2018
parent kdevel <kdevel vogtner.de> writes:
On Saturday, 24 February 2018 at 22:30:09 UTC, Steven 
Schveighoffer wrote:

 The prime example is this:

 byte b = -128;

 int x = -b;

 What would you expect x to be?

 a) 128
 b) -128
Neither nor. I would prefer the codomain of "-" be the range of byte and hence an exception thrown in that case.
Feb 24 2018
prev sibling parent ixid <adamsibson gmail.com> writes:
On Saturday, 24 February 2018 at 20:07:04 UTC, kdevel wrote:
 I don't get the point of the deprecation message:

 --- intprom.d
 import std.stdio;

 void main ()
 {
    short s, t;
    t = -s;
 }
 ---

 $ dmd intprom.d
 intprom.d(6): Deprecation: integral promotion not done for -s, 
 use '-transition=intpromote' switch or -cast(int)(s)

 What shall I do in order to get my template code

 void mymain (T) ()
 {
    :
       b[i] = -b [i];
    :
 }

 compiled for any type for which negation is defined?
It's ridiculous and is going to cause endless pain and spammed or forgotten casts in generic code. It will turn off newbies to D.
Feb 24 2018