mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 14:18:42 +01:00
Closing over "this".
This commit is contained in:
@ -14,6 +14,5 @@
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Closing over this.
|
||||
// TODO: Close over fn/method parameter.
|
||||
// TODO: Maximum number of closed-over variables (directly and/or indirect).
|
||||
// TODO: Shadow variable used in closure.
|
||||
12
test/this/closure.wren
Normal file
12
test/this/closure.wren
Normal file
@ -0,0 +1,12 @@
|
||||
class Foo {
|
||||
getClosure {
|
||||
return fn {
|
||||
return this.toString
|
||||
}
|
||||
}
|
||||
|
||||
toString { return "Foo" }
|
||||
}
|
||||
|
||||
var closure = (new Foo).getClosure
|
||||
io.write(closure.call) // expect: Foo
|
||||
22
test/this/nested_class.wren
Normal file
22
test/this/nested_class.wren
Normal file
@ -0,0 +1,22 @@
|
||||
class Outer {
|
||||
method {
|
||||
io.write(this.toString) // expect: Outer
|
||||
|
||||
fn {
|
||||
io.write(this.toString) // expect: Outer
|
||||
|
||||
class Inner {
|
||||
method {
|
||||
io.write(this.toString) // expect: Inner
|
||||
}
|
||||
toString { return "Inner" }
|
||||
}
|
||||
|
||||
(new Inner).method
|
||||
}.call
|
||||
}
|
||||
|
||||
toString { return "Outer" }
|
||||
}
|
||||
|
||||
(new Outer).method
|
||||
16
test/this/nested_closure.wren
Normal file
16
test/this/nested_closure.wren
Normal file
@ -0,0 +1,16 @@
|
||||
class Foo {
|
||||
getClosure {
|
||||
return fn {
|
||||
return fn {
|
||||
return fn {
|
||||
return this.toString
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toString { return "Foo" }
|
||||
}
|
||||
|
||||
var closure = (new Foo).getClosure
|
||||
io.write(closure.call.call.call) // expect: Foo
|
||||
Reference in New Issue
Block a user