Skip to content

Instantly share code, notes, and snippets.

@masak
Created November 2, 2010 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/660396 to your computer and use it in GitHub Desktop.
Save masak/660396 to your computer and use it in GitHub Desktop.
what Yapsi needs for first-class closures
# 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