Created
November 2, 2010 22:15
-
-
Save masak/660396 to your computer and use it in GitHub Desktop.
what Yapsi needs for first-class closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# block 0 | |
my $c; | |
{ # block 1 | |
my $a = 5; | |
$c = { say $a } # block 2 | |
} | |
$c() | |
We want the above to print "5\n". | |
Here's the thing: | |
* OUTER:: is the block that syntactically surrounds the current block. For | |
example, the OUTER:: of block 1 is block 0, and the OUTER:: of block 2 is | |
block 1. | |
* CALLER:: is the block that called the current block. So block 0 is the | |
CALLER:: of block 1, but in block 1 we don't call block 2, we just store | |
it in a variable. | |
Instead, block 0 is the CALLER:: of block 2. Note that $a has "gone out of | |
scope" by the time we call $c(), but it's still retained because $c has a | |
reference (through block 2's OUTER::) to $a's lexpad. | |
In Yapsi, there's the concept of OUTER:: through the LexPad objects. Each lexpad | |
knows about its outer lexpad. There's no notion of CALLER:: yet. We need that for | |
dynamic variables to work, so we might as well add that now. | |
Also, the last time I tried to make the above code work, I ran into problems because | |
blocks are too special-cased in the compiler. That probably needs some looking-over | |
for the above to ever work. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment