1

I have a genericArray of int

[indent=4]

init
    var a = new GenericArray of int
    for var i = 1 to 3 do a.add (i)
    say: Func of int = def (v)
        stdout.printf ("%d ", v)

    var end = a.length - 1

these all output 1 2 3

for var i = 0 to end do stdout.printf ("%d ", a[i])
for var i = 0 to end do stdout.printf ("%d ", a.data[i])
a.foreach (say)

but this output is 1 0 2 ?

Why the out put is not 1 2 3

for val in a.data do stdout.printf ("%d ", val)

If I set the data field:

var a = new GenericArray of int
a.data = {1, 2, 3}

below: It will output 1 2 3

for val in a.data do stdout.printf ("%d ", val)

everything is fine?

But these all output 1 3 0

for var i = 0 to end do stdout.printf ("%d ", a[i])
for var i = 0 to end do stdout.printf ("%d ", a.data[i])
a.foreach (say)

Why it behave so oddly?

Zee
  • 78
  • 6
  • I can confirm part of your results, but some modification to the code had to be done. First I defined the function say() separately with the syntax: ```def say(v:int) stdout.printf ("%d ", v)```. I couldn`t find the syntax you have used (say: Func of int...) in the tutorials. Indeed the last three lines of code really outputs **1 3 0**, but none outputed **1 0 2**. In fact, the rest of the for loops gives me **1 2 3**. Maybe there is a line of code that you forgot to copy in the question? The one that leads to **1 0 2**, maybe? – lf_araujo Jan 15 '16 at 06:44
  • Hi!, In the first example: this line which output **1 0 2**. `for val in a.data do stdout.printf ("%d ", val)` – Zee Jan 15 '16 at 08:31

0 Answers0