Scoping in a beginner's language:
a := 10
def foo {
a := 20
}
foo()
print a -- should this output 10 or 20?
One approach is require that lexical scoping be requested explicitly:
a := 10
def bar {
use a
a := 30
}
bar()
print a -- would output 30
Or alternatively, if lexical scoping is the default allow a keyword to introduce a new local:
a := 10
def baz {
my a
a := 40
}
baz()
print a -- prints 10, local var did not update outer a
my
can also be used in a block context to create new block locals:
def bleem {
a := 0
while a < 10 {
a := a + 1
}
print a -- prints 10
b := 0
while b < 10 {
my b := 0
b := b + 1
}
-- the above loop will never terminate because
-- b inside the loop is distinct from outer
-- b
}