snow-deprecated icon indicating copy to clipboard operation
snow-deprecated copied to clipboard

Recursive closures behaving oddly.

Open Mikoangelo opened this issue 16 years ago • 2 comments
trafficstars

The following, rather simple, script gives unexpected results:

MyClass: Object.new()
MyClass.object_eval {
  .new: {
    instance: Object.new(self)
    instance.subs: @()
    instance
  }

  .count_it: [level] {
    puts("${level} Calculating count")
    wrapper: Object.new()
    wrapper.count: 1
    .subs.each [sub] {
      wrapper.count: wrapper.count + sub.count_it(level+1)
    }
    puts("${level} Count calculated to be: ${wrapper.count}")
    wrapper.count
  }
}

main: MyClass.new()
main.subs.push(MyClass.new())
main.subs.push(MyClass.new())

puts("Final result: " + main.count_it(0))

Running it with snow gives the following output:

0 Calculating count
1 Calculating count
1 Count calculated to be: 1
2 Calculating count
2 Count calculated to be: 1
0 Count calculated to be: 1
Final result: 1

I would expect level to never reach 2, and the two last lines to end in 2. Currently I'm using a cache in a member to circumvent this issue.

Mikoangelo avatar Jul 03 '09 13:07 Mikoangelo

Looks like a codegen problem. Please provide arch and revision. :)

simonask avatar Jul 03 '09 14:07 simonask

Oh, sorry. Mac OS X 1.5.7, x86_64, 9704c6ac454698a2268f840daaa56c012c239571.

Mikoangelo avatar Jul 03 '09 14:07 Mikoangelo