2 ms·
The effect you are seeing there is because the as it says in the documentation: "The iteration values are assigned to the respective iteration variables as in a
by jgrahamc 14y ago
The effect you are seeing there is because the as it says in the documentation: "The iteration values are assigned to the respective iteration variables as in an assignment statement." So item is being assigned and because it's an obj and not a *obj you are getting a copy.
Whereas slice_of_obj[i] is accessing the element within the slice without a copy. One solution to this problem would be to store pointers in your slice instead of objects.
- jbert 14y agoThanks for the reply. I don't argue that it's not to spec, just that it seems not useful/a bit surprising. Some other languages (e.g. perl and I think also java) choose to make the loop var an alias, rather than a copy, to allow mutation: for my $item (@items) { $item->frob; # Can happily mutate the item } will work as it reads.