Implicit integer casting
Steven Schveighoffer
schveiguy at yahoo.com
Mon Mar 19 04:47:26 PDT 2012
On Sun, 18 Mar 2012 15:53:56 -0400, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> On 3/18/12, Manu <turkeyman at gmail.com> wrote:
>> I'm finding that in this code I'm writing, casts are taking up more
>> space
>> on many lines than the actual term being assigned.
>
> Another classic which fails to compile is:
>
> import std.random;
> ubyte c = uniform(0, 256);
>
> In the call uniform returns a number anywhere from 0 to and including
> 255, which can fit perfectly in a ubyte. But I have to use a cast
> (which is error-prone if I change the right interval), or use a
> to!ubyte call (which is verbose). Granted for simple-purpose random
> number generation a cast might be safe..
What if you don't have std.random's source, all you (the compiler) have is
the function signature?
BTW, you can use this instead of a cast:
ubyte c = uniform(0, 256) & 0xff;
-Steve
More information about the Digitalmars-d
mailing list