I was thinking about scopes (for a problem unrelated to Perl 6), and I
realised that the scoping concepts in P6 are somewhat limited.
We have
my $var # lexical scope
temp $var # lexically-scoped dynamic scope
C<temp> is lexically scoped in that its effect goes away at the closing
curly of the lexical scope that contains it.
A concept that we seem to be missing is the possibility of dynamically
scoped dynamic scopes. I hesitate to come up with a syntax; but I can
think of a couple of examples where it might be used. Caveat: if you
beleive that globals are fundamentally evil, and that everything should
be objects, then this is unnecessary. but for other people ...
Example 1: Create a dynamic scope, and then spawn N threads. Each thread
has its own lexical scope. After each thread has done some work, each
reaches a barrier. Once all the threads have reached that barrier, we
terminate the dynamic scope that we previously introduced: the threads
then continue in their lexical scopes, but with the different dynamic scope
Example 2: a state machine: imagine binding a number of variables into a
"scope space", in which we then instance multiple scopes. We can then
create a state machine in which we change the currently visible values
of the scoped variables by changing the "current scope" of the "scope
space".
One could imagine implementing this by creating the scopes as instances
of an object, and then binding the object's attributes onto the
variables (i.e. "our $foo := $obj.bar"). The "scope space" object would
then be the set of global vaiables to be bound; and the "scope" object
would be the set of values to bind.
However, when we want to release the global vaiables from our scope,
then we need a way to unbind the variables, and restore them to the
bindings that existed before they were bound to our scope space. I'm not
sure how to do that, because we don't have any builtin concept of
dynamically scoped scopes.
Dave.