Closed latk closed 10 years ago
For context:
$ perl6 -e'class Foo { has $!foo = 10; method close_over_foo { return sub { ++$!foo } } }; my $foo = Foo.new; my $closure = $foo.close_over_foo; say $closure.(); say $closure.(); my $foo2 = Foo.new; my $closure2 = $foo2.close_over_foo; say $closure2.(); say $closure2.()'
11
12
11
12
Also, even if we can't get this to work in the prototype, we should definitely not allow undef to be used as the instance when looking up attribute values - this should either do the right thing or die.
That previous p6 snippet wasn't actually demonstrating the correct code, it should actually be this:
$ perl6 -e'class Foo { has $!foo = 10; method close_over_foo { return sub { ++$!foo } } }; my $closure = Foo.new.close_over_foo; say $closure.(); say $closure.(); my $closure2 = Foo.new.close_over_foo; say $closure2.(); say $closure2.()'
11
12
11
12
Given the simple class
I would expect the code
to output
1 1 2
. However, we get1 2 3
and a load of errors, because$instance
isundef
at line 90 of attribute.pm.The invocant which the anonymous sub has closed over is not available in the dynamic scope of the
$c1
invocation, therefore attribute lookup does not seem to work.