"For" infinite loop

MattCoder mattcoder at hotmail.com
Mon Aug 13 16:41:11 PDT 2012


On Monday, 13 August 2012 at 08:18:08 UTC, Marco Leise wrote:
> Am Mon, 13 Aug 2012 00:53:43 +0200
> schrieb "bearophile" <bearophileHUGS at lycos.com>:
>
>> It looks better. But you every time you use a cast in D you 
>> need to be careful, because they are like a sharp tool with 
>> almost no safeties.
>> 
>> Bye,
>> bearophile
>
> So true. It happens to me sometimes that I cast away const 
> although I just want to cast a type. It's also a reason I am 
> not a big fan of considering "cast(ubyte) 0" normal. It is 
> pragmatic though.

In fact, it would be nice to do something like this:

foreach(ubyte i; 0..256)
     writefln("%d", i);

but the code above will generate an error like: 256 cannot be 
converted to ubyte!

On the other hand:

foreach(ubyte i; 0..255)
     writefln("%d", i);

Works, but only will show numbers from 0..254, without "255"!

So, maybe this is more safer:

foreach(ushort i; 0..ubyte.max+1)
     writefln("%d", cast(ubyte)i);

Matheus.


More information about the Digitalmars-d-learn mailing list