Skip to content

Instantly share code, notes, and snippets.

@eranws
Created May 28, 2017 23:01
Show Gist options
  • Save eranws/9cef23d4664079f4b2b593b378b180ad to your computer and use it in GitHub Desktop.
Save eranws/9cef23d4664079f4b2b593b378b180ad to your computer and use it in GitHub Desktop.
rrrrr (red/rebol resources and rough reasonings)

Zen and the Art of Scope Maintenance: An Inquiry into Contexts

Context is essentialy a namespace

Two key points to get it:

  1. Every word is bound to global context by default at load time (source as text to Red values).
  2. Local contexts are constructed at run-time.
  • Each context constructor will determine which words it should capture by either looking into a declared list (locals in functions), or gather all the set-words (objects).
  • Then, the constructor will go through the nested blocks in the body to bind all the “local” words. When applied in nested way object [... object [... func [...][...]]], the various binding passes will produce a result which mimic nested scopes.
  • That is what we call definitional scoping.

With dynamic scoping we have chains of function calls to traverse, with static scoping we have chains of definitions, but with definitional “scoping” there’s no chains at all, hence there’s no spoon scopes! Multiple rounds of bindings give an illusion of nesting (i.e. static scoping), while in fact all contexts are completely separate, w/o any hierarchy between them.

Red type hierarchy from @toomasv

any-type!
    event!
    unset!
    any-function!
        native! action! op! function! routine!
    any-object!
        object! error!
    series!
        any-block!
            any-list!
                block! paren! hash!
            any-path!
                path! lit-path! set-path! get-path!
        any-string!
            string! file! url! tag! email!
        binary!
        image!
        vector!
    immediate!
        any-word!
            word! set-word! lit-word! get-word! refinement! issue!
        scalar!
            number!
                integer! float! percent!
            char!
            pair!
            tuple!
            time!
        datatype!
        none!
        logic!
        typeset!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment