Re: Changing elements during foreach

2013-10-21 Thread qznc
On Monday, 21 October 2013 at 20:14:09 UTC, ixid wrote: On Monday, 21 October 2013 at 19:37:47 UTC, Jonathan M Davis wrote: On Monday, October 21, 2013 21:16:00 qznc wrote: On Monday, 21 October 2013 at 16:22:29 UTC, Krzysztof Ciebiera wrote: > I understand slices now and I don't find it consi

Re: Changing elements during foreach

2013-10-21 Thread Jonathan M Davis
On Monday, October 21, 2013 22:14:08 ixid wrote: > What would be the issue/s with disallowing appending to slices? > So you'd have to explicitly duplicate before you could append. All arrays are slices. There is no difference between the two. It's just that if you start appending to one, it will

Re: Changing elements during foreach

2013-10-21 Thread ixid
On Monday, 21 October 2013 at 19:37:47 UTC, Jonathan M Davis wrote: On Monday, October 21, 2013 21:16:00 qznc wrote: On Monday, 21 October 2013 at 16:22:29 UTC, Krzysztof Ciebiera wrote: > I understand slices now and I don't find it consistent with > "no > shoot in the foot by default" statem

Re: Changing elements during foreach

2013-10-21 Thread Jonathan M Davis
On Monday, October 21, 2013 21:16:00 qznc wrote: > On Monday, 21 October 2013 at 16:22:29 UTC, Krzysztof Ciebiera > > wrote: > > I understand slices now and I don't find it consistent with "no > > shoot in the foot by default" statement. > > I agree. The pitfalls are well understood, yet everybod

Re: Changing elements during foreach

2013-10-21 Thread Dicebot
On Monday, 21 October 2013 at 16:22:29 UTC, Krzysztof Ciebiera wrote: What I want to do is: I want to take a look at every single solution and extend it if it is possible (eg. if solution contains obbects a,b,c it can also contain d). void main() { import std.stdio; int[][] dat

Re: Changing elements during foreach

2013-10-21 Thread qznc
On Monday, 21 October 2013 at 16:22:29 UTC, Krzysztof Ciebiera wrote: I understand slices now and I don't find it consistent with "no shoot in the foot by default" statement. I agree. The pitfalls are well understood, yet everybody seems to love them. Ok, compared to C array they are an improv

Re: Changing elements during foreach

2013-10-21 Thread Krzysztof Ciebiera
On Monday, 21 October 2013 at 14:59:54 UTC, Alexandr Druzhinin wrote: If you switch instruction order you create local copy and then set x[0] in local copy so original is unchanged. But local copy creating depends on several thing and happens not every appending in general. Your way is not D-is

Re: Changing elements during foreach

2013-10-21 Thread Alexandr Druzhinin
21.10.2013 17:55, Krzysztof Ciebiera пишет: On Monday, 21 October 2013 at 10:41:38 UTC, Alexandr Druzhinin wrote: 21.10.2013 17:31, Krzysztof Ciebiera пишет: void main() { int a[][] = [[1,2,3]]; foreach(x; a) { x[0] = 0; x ~= 4; } writeln(a); } ... &) or [0,2

Re: Changing elements during foreach

2013-10-21 Thread bearophile
Krzysztof Ciebiera: Is the following compiler behavior consistent with language specification? Changing elements during foreach is something to avoid, perhaps I'd like it to be statically forbidden. If you add to this the reference-struct nature of arrays, you get in troubles.

Re: Changing elements during foreach

2013-10-21 Thread qznc
On Monday, 21 October 2013 at 10:31:51 UTC, Krzysztof Ciebiera wrote: Is the following compiler behavior consistent with language specification? import std.stdio; void main() { int a[][] = [[1,2,3]]; foreach(x; a) { x[0] = 0; x ~= 4; } writeln(a); } I under

Re: Changing elements during foreach

2013-10-21 Thread Krzysztof Ciebiera
On Monday, 21 October 2013 at 10:41:38 UTC, Alexandr Druzhinin wrote: 21.10.2013 17:31, Krzysztof Ciebiera пишет: void main() { int a[][] = [[1,2,3]]; foreach(x; a) { x[0] = 0; x ~= 4; } writeln(a); } ... &) or [0,2,3,4] (python, C++ ref). But [0,2,3]? It was

Re: Changing elements during foreach

2013-10-21 Thread Alexandr Druzhinin
21.10.2013 17:31, Krzysztof Ciebiera пишет: Is the following compiler behavior consistent with language specification? import std.stdio; void main() { int a[][] = [[1,2,3]]; foreach(x; a) { x[0] = 0; x ~= 4; } writeln(a); } I understand why thw progra

Changing elements during foreach

2013-10-21 Thread Krzysztof Ciebiera
Is the following compiler behavior consistent with language specification? import std.stdio; void main() { int a[][] = [[1,2,3]]; foreach(x; a) { x[0] = 0; x ~= 4; } writeln(a); } I understand why thw program could output [1,2,3] (like in C++ without &) or