Skip to content

Instantly share code, notes, and snippets.

@jaz303
Last active August 29, 2015 14:10
Show Gist options
  • Save jaz303/3d5ee3fb84c92e4c38b0 to your computer and use it in GitHub Desktop.
Save jaz303/3d5ee3fb84c92e4c38b0 to your computer and use it in GitHub Desktop.

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
    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment