Leaking I
I’m designing a programming language called Tainted Oyster.
One thing I have to think about is how to set a variable in the current scope. The easy way is the straightforward way:
a ← 25
This pairs the value 25 with the symbol a, in the scope in which it’s written. (The cheap way to say that is, “Now a is 25.” Unfortunately, I’m kinda slow, and when I’m directly discussing the value bound to the same symbol in different scopes, little lies like “is” often trap me.)
But I want to be able to abstract-ify assignment; for instance, in writing the procedure def. def creates a function and assigns it to a symbol in the current scope:
def fib (val):
if (val <= 0):
1
val + fib (val - 1)
For the moment, though, let me stick to a simpler example. Here’s the skeleton of a function that tries to set a to 5 times a number:
def set-a-to-5-times (b):
# do some things
set-a-to-5-times 6
# now a == 30
What should go in the place of the comment do some things? There are two possibilities that I have experienced. Some languages work this way: if the symbol you are assigning has not already been declared in the current scope, then it will be assigned in the global scope. Javascript works this way. This, I will put forward, is balderdash. It is foul because it prevents you from wrapping an unknown block of code in a scope in order to isolate it.
The other option is that you can only set variables inside the current scope. This is a neat, functional approach, that is easy to reason about —— but it lacks the power to write set-a-to-5-times at all.
Oyster takes this tack: normally, scopes encapsulate completely —- like the second option. But scopes can have holes explicitly poked in them. I call a hole a “leak”, and use a procedure called leak. leak with one argument does just what I need:
def set-a-to-5-times (b):
leak a
a ← 5 * b
set-a-to-5-times 6
print a # prints 30
Here, leak pokes an a-shaped hole in foo’s scope, and lets the assignment act as if it were one level up. Without leak, the print statement would signal an error, due to an unbound symbol a.
That’s leaking (with one argument).
Posted on 6 October 2011