+ operators

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 11 19:03:49 PDT 2011


On 2011-06-11 18:54, Steven Schveighoffer wrote:
> On Sat, 11 Jun 2011 18:32:59 -0400, Jonathan M Davis <jmdavisProg at gmx.com>
> 
> wrote:
> > On 2011-06-11 14:54, Renoir wrote:
> >> Sorry for the question but i'm an absolutely noob
> >> I have:
> >> 
> >> byte x = 10;
> >> byte y = 3;
> >> x = x + y;
> >> 
> >> why compilers complains?
> >> 
> >> Error: cannot implicitly convert expression (cast(int)x + cast(int)
> >> y
> >> ) of type int to byte
> >> 
> >> Have i to make another cast to sum byte + byte?
> > 
> > All integral operations where the types are int or smaller result in an
> > int
> > (unless you're dealing with unsigned types, in which case, I believe
> > that the
> > result would be uint). So, in this case the result of x + y is int. So,
> > if you
> > want the result to be byte, you have to do
> > 
> > x = cast(byte)(x + y);
> 
> If I'm not mistaken, the original code should be handled (and compile
> without errors) by range propagation. Is that not fully implemented?

No. As I understand it, some of the simple cases have been (though you would 
have thought that this would qualify as a simple case), but IIRC, when it came 
up recently, Don said that it wasn't fully implemented yet. I'm not quite sure 
which parts of it have been and haven't been implemented though.

Certainly, once range propagation has been fully implemented, this particular 
will work without needing any casts, but as soon as the compiler doesn't know 
what the values of x and y are, I believe that it would still end up 
complaining.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list