I'm having some trouble incrementing a variable integer. This code:
variable Integer myInteger = -1;
Integer getInteger () {
myInteger = myInteger + 1;
print("myInteger: " + myInteger.string);
return Integer(myInteger);
}
Array<Integer> integers =
Array.ofSize(
4,
getInteger());
print(integers);
Gives this output:
myInteger: 0
{ 0, 0, 0, 0 }
while the intended output was:
myInteger: 0
myInteger: 1
myInteger: 2
myInteger: 3
{ 0, 1, 2, 3 }
What's up with that?
Your example, which I assume is contrived, can be written as
Array(0:4)
orArray(0..3)
. But assuming that you have some good reason to want to loop over a generating function, here is how I would write it:Or, equivalently:
Or even:
That's much better, IMO, than using a stream which accesses a variable in an outer scope.