Why there is too many uneccessary casts?

Simen Kjaeraas simen.kjaras at gmail.com
Tue Jun 11 03:30:44 PDT 2013


On Tue, 11 Jun 2013 12:12:25 +0200, Temtaime <temtaime at gmail.com> wrote:

> ubyte k = 10;
> ubyte c = k + 1;
>
> This code fails to compile because of: Error: cannot implicitly convert  
> expression (cast(int)k + 1) of type int to ubyte
>
> Why? It's pain in the ass, i think. My code contains only casts then.

Because it's unsafe. The compiler does not know if k+1 fits in a ubyte.

(True, in this case it could know it, but it does not, and in the general
case it can't know)

-- 
Simen


More information about the Digitalmars-d-learn mailing list