Nested, named closures don't work #501

Closed
nddrylliog opened this Issue Dec 6, 2012 · 5 comments

Comments

Projects
None yet
2 participants
Member

nddrylliog commented Dec 6, 2012

This fails to compile:

main: func {

  a := 42

  f1 := func -> Int {
    b := 26
    f2 := func -> Int {
      a + b
    }
    f2()
  }

  "%d" printfln(f1())
}

One of the problems is that f2 is 'captured' in the context of f1.. but it shouldn't, because it's also declared in f1 itself.

Member

nddrylliog commented Dec 6, 2012

Note that the test was originally there to show that we couldn't access a from f2 because it was referenced 2 levels deep, which rock doesn't like

Collaborator

shamanas commented Dec 9, 2012

By the way, i think the n-references deep vars not captured bug was fixed a while back.
For example, this works well:

foo: func(f: Func) { f() }
a := 42
foo(|| foo(|| a toString() println()))

shamanas was assigned Dec 22, 2012

shamanas closed this in 67ef9be Dec 23, 2012

Member

nddrylliog commented Dec 23, 2012

@shamanas This seems to be a step in the right direction. I haven't tested it yet tho :)

I'm pretty sure there are still closure bugs left, see for example this workaround: https://github.com/nddrylliog/dye/blob/master/source/dye/sprite.ooc#L106 - I'm away from a rock install now, can you check if it's still needed?

Collaborator

shamanas commented Dec 23, 2012

You are basically forcing to this to be captured in the closure right?

It seems to still be broken, here is the testcase I used:

apply: func(f: Func) { f() }
Foo: class {
    x: Int
    init: func(=x)
    bar: func {
        apply(||
            apply(||
                x toString() println()
            )
        )
    }
}
Foo new(42) bar()

(Works with your workaround obviously)

Member

nddrylliog commented Dec 24, 2012

Yup, I'm forcing this. This is the usual workaround for rebel closures :) But of course not needing workaround is better!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment