Dynamically Scoped Dynamic Scopes

3 views
Skip to first unread message

Dave Whipp

unread,
Feb 27, 2005, 1:52:58 PM2/27/05
to perl6-l...@perl.org
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.

Larry Wall

unread,
Mar 1, 2005, 8:38:50 PM3/1/05
to perl6-l...@perl.org
On Sun, Feb 27, 2005 at 10:52:58AM -0800, Dave Whipp wrote:
: 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.

Hmm. Just sounds to me like a form of tying involving delegation.
Piece o' cake. Syntax left for the module author to decide, if a
special declarator is needed in preference to standard variable-type
syntax.

Larry

Reply all
Reply to author
Forward
0 new messages