August 12, 2012
On Saturday, 11 August 2012 at 20:38:33 UTC, bearophile wrote:
> bioinfornatics:
>
>> n this case why not using a while loop ?
>
> It uses less lines of code, and with the for loop you have a single place where to put the loop variable initialization, test and increment. This makes the code simpler to read. In this case the test is moved inside the loop, but it's better still than a regular while loop.
>
> Bye,
> bearophile

What about:

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

It may look better than for loop. ("Maybe").
August 12, 2012
MattCoder:

> foreach(ushort i; 0..256)
>   writefln("%d", cast(ubyte)i);
>
> It may look better than for loop. ("Maybe").

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
August 13, 2012
Am Mon, 13 Aug 2012 00:53:43 +0200
schrieb "bearophile" <bearophileHUGS@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.

-- 
Marco

August 13, 2012
Marco Leise:

Again it seems my message you have answered to is not visible through the online reader at forum.dlang.org.

> 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.

A not a lot documented way to remove a const it to use an empty cast() with no argument. Another way is to use unqual!().

Bye,
bearophile
August 13, 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@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.
1 2
Next ›   Last »