If I'm wrong please correct me, but it would seem that JMP has dynamic-scope with no scope persistence (except for the pseudo-namespaces Here
, Window
, Box
). This contrasts with many popular programming languages that have lexical-scope with full scope-persistence using reference-counters.
This caused a lot of head-smashing on my end until I gained the intuition needed to work with the weird scope system that JMP has, and I believe that it's a large point of confusion for many people. Not many programming languages use Dynamic scope (LISP being an important exception, and JSL has some similarities to LISP).
I'd encourage the JSL documentation team to point out this distinction and provide some resources / links on how to better understand Dynamic scope as it certainly is not the standard way of doing name-resolution.
Here is an example -- the outer function's local scope is cleared up immediately after the return statement, and the inner functions name-resolution is dynamic (based on the current call-stack):
Names Default to Here( 1 );
x = 7;
outer function = Function( {},
{x},
x = 4;
inner function = Function( {},
Print( x )
);
Return( Name Expr( inner function ) );
);
func = outer function;
func()
/*** OUTPUT ***
7
If you do something functionally equivalent in a lexically-scoped language with scope-persistence (like Python), then the inner function's enclosing-scope is persistent (it's the local-scope for the outer function), and the name-resolution for the inner function's x-variable is the value 4 from the lexical reading of the system. Since JMP doesn't use lexical-scope, but uses dynamic-scope, then when the inner function is finally executed at the end of the script, it uses dynamic name-resolution of Local -> Here -> Global (and possible column-names for the current table) and finds the value 7.
Understanding this type of behavior is critical for building robust programs in JSL, and I just want to point it out to anyone learning / struggling with JSL.
Let me know if you have any thoughts / unintuitive examples!
Jordan