digitalmars.D.learn - Array initialization quiz
- bearophile (10/10) Nov 28 2011 A little D2 quiz, of course. This was discussed some time ago, but repea...
- Andrej Mitrovic (1/1) Nov 28 2011 I get an infinite loop. :s
- bearophile (4/5) Nov 28 2011 In your brain, really? Is that dangerous?
- Andrej Mitrovic (1/1) Nov 28 2011 Err no? Running after compilation goes into an infinite loop on 2.056.
- bearophile (5/6) Nov 28 2011 Silly, this is a quiz, you need to try to answer without compiling it fi...
- Andrej Mitrovic (3/5) Nov 28 2011 Because foreach is broken?
- deadalnix (5/10) Nov 29 2011 No it has nothing to do with this bug. But actually, this exemple should...
- bearophile (5/8) Nov 29 2011 I'd like that code to loop on all array 256 items once, and then stop :-...
- Marco Leise (2/10) Nov 29 2011 Is it evil if I propose that 'i' should always be size_t and nothing els...
- Marco Leise (2/20) Nov 29 2011 Yes, because you can iterate over -10..10 as well -.-
- Jonathan M Davis (8/29) Nov 29 2011 I would argue that i should always be size_t if it's an index. So,
- Steven Schveighoffer (14/43) Nov 30 2011 The type of the index should be irrelavent to the underlying loop
- Xinok (9/23) Nov 30 2011 This actually doesn't compile:
- Steven Schveighoffer (12/40) Nov 30 2011 This is hugely inefficient...
- Xinok (8/51) Nov 30 2011 It shouldn't be. The compiler should be smart enough to inline the
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/31) Nov 30 2011 But variables may be implemented on CPU registers and be fast. That is
- Andrej Mitrovic (4/7) Nov 30 2011 Instant flashback, I think it was this:
- Andrej Mitrovic (1/1) Nov 29 2011 I meant that in a sarcastic way. :)
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
Nov 28 2011
Andrej Mitrovic:I get an infinite loop. :sIn your brain, really? Is that dangerous? Bye, bearophile
Nov 28 2011
Err no? Running after compilation goes into an infinite loop on 2.056.
Nov 28 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
Nov 28 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
Nov 28 2011
Le 29/11/2011 04:11, Andrej Mitrovic a écrit :On 11/29/11, bearophile<bearophileHUGS lycos.com> wrote: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.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
Nov 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
Nov 29 2011
Am 29.11.2011, 14:53 Uhr, schrieb bearophile <bearophileHUGS lycos.com>:deadalnix:Is it evil if I propose that 'i' should always be size_t and nothing else?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
Nov 29 2011
Am 29.11.2011, 20:41 Uhr, schrieb Marco Leise <Marco.Leise gmx.de>:Am 29.11.2011, 14:53 Uhr, schrieb bearophile <bearophileHUGS lycos.com>:Yes, because you can iterate over -10..10 as well -.-deadalnix:Is it evil if I propose that 'i' should always be size_t and nothing else?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
Nov 29 2011
On Tuesday, November 29, 2011 20:42:59 Marco Leise wrote:Am 29.11.2011, 20:41 Uhr, schrieb Marco Leise <Marco.Leise gmx.de>:I would argue that i should always be size_t if it's an index. So, foreach(i; -10 .. 10) {} would be valid, but int[] arr; foreach(byte i, val; arr) {} wouldn't be. - Jonathn M DavisAm 29.11.2011, 14:53 Uhr, schrieb bearophile <bearophileHUGS lycos.com>:Yes, because you can iterate over -10..10 as well -.-deadalnix:Is it evil if I propose that 'i' should always be size_t and nothing else?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
Nov 29 2011
On Tue, 29 Nov 2011 15:06:11 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote:On Tuesday, November 29, 2011 20:42:59 Marco Leise wrote:The type of the index should be irrelavent to the underlying loop mechanism. Note that the issue is really that foreach(T i, val; arr) {...} translates to for(T i = 0; i < arr.length; ++i) {auto val = arr[i]; ...} Why can't it be (aside from the limitation in for-loop syntax, but you get the idea): for(size_t _i = 0, T i = _i; _i < arr.length; i = ++_i) {auto val = arr[_i]; ...} Same issue with foreach(i; -10..10), what if I wanted to do foreach (ubyte i; ubyte.min..ubyte.max + 1). This should not result in an infinite loop, I should be able to use foreach to iterate all the values of ubyte. The compiler should just "figure out" how to do it right. -SteveAm 29.11.2011, 20:41 Uhr, schrieb Marco Leise <Marco.Leise gmx.de>:I would argue that i should always be size_t if it's an index. So, foreach(i; -10 .. 10) {} would be valid, but int[] arr; foreach(byte i, val; arr) {} wouldn't be.Am 29.11.2011, 14:53 Uhr, schrieb bearophile<bearophileHUGS lycos.com>:Yes, because you can iterate over -10..10 as well -.-deadalnix:Is it evil if I propose that 'i' should always be size_t and nothing else?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
Nov 30 2011
On 11/30/2011 7:50 AM, Steven Schveighoffer wrote:On Tue, 29 Nov 2011 15:06:11 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote: The type of the index should be irrelavent to the underlying loop mechanism. Note that the issue is really that foreach(T i, val; arr) {...} translates to for(T i = 0; i < arr.length; ++i) {auto val = arr[i]; ...} Why can't it be (aside from the limitation in for-loop syntax, but you get the idea): for(size_t _i = 0, T i = _i; _i < arr.length; i = ++_i) {auto val = arr[_i]; ...} Same issue with foreach(i; -10..10), what if I wanted to do foreach (ubyte i; ubyte.min..ubyte.max + 1). This should not result in an infinite loop, I should be able to use foreach to iterate all the values of ubyte. The compiler should just "figure out" how to do it right. -SteveThis actually doesn't compile: foreach (ubyte i; ubyte.min..ubyte.max + 1) Error: cannot implicitly convert expression (256) of type int to ubyte It's a little more to type, but just write a property which does an explicit cast: foreach(_i; ubyte.min .. ubyte.max + 1){ ubyte i(){ return cast(ubyte)_i; } }
Nov 30 2011
On Wed, 30 Nov 2011 10:54:11 -0500, Xinok <xinok live.com> wrote:On 11/30/2011 7:50 AM, Steven Schveighoffer wrote:This is hugely inefficient... foreach(_i; ubyte.min..ubyte.max + 1){ ubyte i = cast(ubyte)_i; } But my point was, foreach over a range gives me all the elements in a range, regardless of how the underlying loop is constructed. The limitation by the compiler is artificial. I remember at one point there was someone who had actual code that resulted in a loop for ubytes, or was trying to figure out how to foreach over all possible ubyte values. -SteveOn Tue, 29 Nov 2011 15:06:11 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote: The type of the index should be irrelavent to the underlying loop mechanism. Note that the issue is really that foreach(T i, val; arr) {...} translates to for(T i = 0; i < arr.length; ++i) {auto val = arr[i]; ...} Why can't it be (aside from the limitation in for-loop syntax, but you get the idea): for(size_t _i = 0, T i = _i; _i < arr.length; i = ++_i) {auto val = arr[_i]; ...} Same issue with foreach(i; -10..10), what if I wanted to do foreach (ubyte i; ubyte.min..ubyte.max + 1). This should not result in an infinite loop, I should be able to use foreach to iterate all the values of ubyte. The compiler should just "figure out" how to do it right. -SteveThis actually doesn't compile: foreach (ubyte i; ubyte.min..ubyte.max + 1) Error: cannot implicitly convert expression (256) of type int to ubyte It's a little more to type, but just write a property which does an explicit cast: foreach(_i; ubyte.min .. ubyte.max + 1){ ubyte i(){ return cast(ubyte)_i; } }
Nov 30 2011
On 11/30/2011 11:46 AM, Steven Schveighoffer wrote:On Wed, 30 Nov 2011 10:54:11 -0500, Xinok <xinok live.com> wrote:It shouldn't be. The compiler should be smart enough to inline the function and optimize the typecast to something like this: void main(string[] args){ foreach(_i; ubyte.min .. ubyte.max + 1) writeln(*cast(ubyte*)&_i); } This would actually be faster since you don't have to iterate two variables.On 11/30/2011 7:50 AM, Steven Schveighoffer wrote:This is hugely inefficient... foreach(_i; ubyte.min..ubyte.max + 1){ ubyte i = cast(ubyte)_i; } But my point was, foreach over a range gives me all the elements in a range, regardless of how the underlying loop is constructed. The limitation by the compiler is artificial. I remember at one point there was someone who had actual code that resulted in a loop for ubytes, or was trying to figure out how to foreach over all possible ubyte values. -SteveOn Tue, 29 Nov 2011 15:06:11 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote: The type of the index should be irrelavent to the underlying loop mechanism. Note that the issue is really that foreach(T i, val; arr) {...} translates to for(T i = 0; i < arr.length; ++i) {auto val = arr[i]; ...} Why can't it be (aside from the limitation in for-loop syntax, but you get the idea): for(size_t _i = 0, T i = _i; _i < arr.length; i = ++_i) {auto val = arr[_i]; ...} Same issue with foreach(i; -10..10), what if I wanted to do foreach (ubyte i; ubyte.min..ubyte.max + 1). This should not result in an infinite loop, I should be able to use foreach to iterate all the values of ubyte. The compiler should just "figure out" how to do it right. -SteveThis actually doesn't compile: foreach (ubyte i; ubyte.min..ubyte.max + 1) Error: cannot implicitly convert expression (256) of type int to ubyte It's a little more to type, but just write a property which does an explicit cast: foreach(_i; ubyte.min .. ubyte.max + 1){ ubyte i(){ return cast(ubyte)_i; } }
Nov 30 2011
On 11/30/2011 10:17 AM, Xinok wrote:On 11/30/2011 11:46 AM, Steven Schveighoffer wrote:But variables may be implemented on CPU registers and be fast. That is easy to do with Steven's code and cast above. In your code, taking the address of _i would put _i in memory. (Unless the compiler has an optimization for that. Too lazy to check. :() Also, *cast(ubyte*)&_i works only on little endian systems, right? But you meant that the compiler should take care of endianness as well. Aliforeach(_i; ubyte.min..ubyte.max + 1){ ubyte i = cast(ubyte)_i; } But my point was, foreach over a range gives me all the elements in a range, regardless of how the underlying loop is constructed. The limitation by the compiler is artificial. I remember at one point there was someone who had actual code that resulted in a loop for ubytes, or was trying to figure out how to foreach over all possible ubyte values. -SteveIt shouldn't be. The compiler should be smart enough to inline the function and optimize the typecast to something like this: void main(string[] args){ foreach(_i; ubyte.min .. ubyte.max + 1) writeln(*cast(ubyte*)&_i); } This would actually be faster since you don't have to iterate two variables.
Nov 30 2011
On 11/30/11, Steven Schveighoffer <schveiguy yahoo.com> wrote:I remember at one point there was someone who had actual code that resulted in a loop for ubytes, or was trying to figure out how to foreach over all possible ubyte values.Instant flashback, I think it was this: http://www.digitalmars.com/d/archives/digitalmars/D/learn/Foreach_with_byte_problems_24997.html :)
Nov 30 2011
I meant that in a sarcastic way. :)
Nov 29 2011