Inferring an integer literal as ubyte

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 14 06:18:12 PST 2015


On Monday, 14 December 2015 at 13:33:41 UTC, Shriramana Sharma 
wrote:
> ubyte code = to!ubyte(spec, 6) + 16;

That's not an integer literal... that's a runtime value of ubyte 
plus an integer literal.

Since the ubyte is the result of a runtime function, the compiler 
doesn't know what it will be and thinks it could be anything from 
0 to 255, inclusive.

240 + 16 = 256 = too big to fit in a ubyte, to it requires a cast.

ubyte code = 16; // this would work

ubyte code = 239 + 16; // this too

but yours won't because to!ubyte(spec, 6) might just be > 240.


More information about the Digitalmars-d-learn mailing list