Pop Quiz what is wrong with this code?

rikki cattermole rikki at cattermole.co.nz
Sat Jun 20 16:20:45 UTC 2020


On 21/06/2020 4:00 AM, Danni Coy wrote:
> On Sun, Jun 21, 2020 at 1:15 AM rikki cattermole via Digitalmars-d
> <digitalmars-d at puremagic.com> wrote:
>>
>> On 21/06/2020 3:08 AM, Danni Coy wrote:
>>> ...
>>> foreach (y, ref row; offsetMap)
>>> {
>>>      auto run = 0;
>>>       auto nPixels = 0;
>>>       foreach (x; 0..size.x)
>>>       {
>>>            immutable SDL_FPair offset  = { center.x - x, y - center.y };
>>> ...
>>>
>>> and why does it violate the principle of making the right thing the
>>> easy thing to do?
>>>
>>
>> oo oo, is it that you didn't want x and y to be of type size_t?
> more specifically I didn't want them to be unsigned
> I am adapting the code from C++ code,
> coming from other Algol syntax languages. That one is a real
> gotcha. I tried explicitly making x and y ints and I got a depreciation warning.

You are performing an operation on memory, size_t is indeed what you 
wanted. It is an offset into the address space past another offset (the 
pointer).

You cannot fit the entire address space into an int, but a size_t value 
is guaranteed to be able to do this.

Hence size_t is the correct data type for indexes over a slice.

>> And why are you initializing run and nPixels manually?
> the value there so the compiler can figure out the type (auto)

You don't need to do this.

All D types are default initialized to a value.

int in this case will default initialize to 0, which is what you are 
doing explicitly.


More information about the Digitalmars-d mailing list