Jump to page: 1 2
Thread overview
Array initialization quiz
Nov 29, 2011
bearophile
Nov 29, 2011
Andrej Mitrovic
Nov 29, 2011
bearophile
Nov 29, 2011
Andrej Mitrovic
Nov 29, 2011
bearophile
Nov 29, 2011
Andrej Mitrovic
Nov 29, 2011
deadalnix
Nov 29, 2011
bearophile
Nov 29, 2011
Marco Leise
Nov 29, 2011
Marco Leise
Nov 29, 2011
Jonathan M Davis
Nov 30, 2011
Xinok
Nov 30, 2011
Xinok
Nov 30, 2011
Ali Çehreli
Nov 30, 2011
Andrej Mitrovic
Nov 29, 2011
Andrej Mitrovic
November 29, 2011
A little D2 quiz, of course. This was discussed some time ago, but repeating now and then such things doesn't hurt in D.learn.

Do you think this little program compiles? Or do you think the D2 compiler will refuse to compile it and ask you for a cast from int -> ubyte?

If this compiles (maybe because you have added a cast at line 4) do you think this program is working correctly?


void main() {
    ubyte[256] table;
    foreach (ubyte i, ref x; table)
        x = 255 - i;
}

Bye,
bearophile
November 29, 2011
I get an infinite loop. :s
November 29, 2011
Andrej Mitrovic:

> I get an infinite loop. :s

In your brain, really? Is that dangerous?

Bye,
bearophile
November 29, 2011
Err no? Running after compilation goes into an infinite loop on 2.056.
November 29, 2011
Andrej Mitrovic:

> Err no? Running after compilation goes into an infinite loop on 2.056.

Silly, this is a quiz, you need to try to answer without compiling it first :-)

Do you know why the compiler doesn't ask you for a cast, and why the run does that?

Bye,
bearophile
November 29, 2011
On 11/29/11, bearophile <bearophileHUGS@lycos.com> wrote:
> Do you know why the compiler doesn't ask you for a cast, and why the run does that?

Because foreach is broken? http://d.puremagic.com/issues/show_bug.cgi?id=4510
November 29, 2011
Le 29/11/2011 04:11, Andrej Mitrovic a écrit :
> On 11/29/11, bearophile<bearophileHUGS@lycos.com>  wrote:
>> Do you know why the compiler doesn't ask you for a cast, and why the run
>> does that?
>
> Because foreach is broken?
> http://d.puremagic.com/issues/show_bug.cgi?id=4510

No it has nothing to do with this bug. But actually, this exemple should generate a warning at least, or being illegal eventually. I being ubyte, the max value is 255, so you always ends up with a valid value of i and the loop never stop.
November 29, 2011
deadalnix:

> No it has nothing to do with this bug.

I tend to agree.


> But actually, this exemple should generate a warning at least, or being illegal eventually.

I'd like that code to loop on all array 256 items once, and then stop :-)

Bye,
bearophile
November 29, 2011
I meant that in a sarcastic way. :)
November 29, 2011
Am 29.11.2011, 14:53 Uhr, schrieb bearophile <bearophileHUGS@lycos.com>:

> deadalnix:
>
>> No it has nothing to do with this bug.
>
> I tend to agree.
>
>
>> But actually, this exemple should
>> generate a warning at least, or being illegal eventually.
>
> I'd like that code to loop on all array 256 items once, and then stop :-)
>
> Bye,
> bearophile

Is it evil if I propose that 'i' should always be size_t and nothing else?
« First   ‹ Prev
1 2