Re: short s, t; t = -s: no (longer) works: Deprecation: integral promotion not done for -s, use

2018-02-24 Thread ixid via Digitalmars-d-learn

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.


Re: short s, t; t = -s: no (longer) works: Deprecation: integral promotion not done for -s, use

2018-02-24 Thread kdevel via Digitalmars-d-learn
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.


Re: short s, t; t = -s: no (longer) works: Deprecation: integral promotion not done for -s, use

2018-02-24 Thread Steven Schveighoffer via Digitalmars-d-learn

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


Re: short s, t; t = -s: no (longer) works: Deprecation: integral promotion not done for -s, use

2018-02-24 Thread kdevel via Digitalmars-d-learn
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?






Re: short s, t; t = -s: no (longer) works: Deprecation: integral promotion not done for -s, use

2018-02-24 Thread Steven Schveighoffer via Digitalmars-d-learn

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


short s, t; t = -s: no (longer) works: Deprecation: integral promotion not done for -s, use

2018-02-24 Thread kdevel via Digitalmars-d-learn

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?