Perform lexical binding of $0 in functions
#240
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #239.
Es' historical behavior with
$0is to bind it, dynamically, when invoking a lambda invoked via a function name. This creates confusion, as most obviously demonstrated bybut also illustrated by a number of other examples in #239.
This PR changes the binding of
$0from dynamic to lexical, such that when we look up a function, we bind$0to the function name within the lexical scope of the looked-up definition. This creates good, intuitive behavior in basically all cases, particularly with respect to functions which take other code as arguments. It also removes the behavioral differences betweenfn-x = @ {}andfn-x = $&noreturn @ {}.$0in settor functions also uses this new behavior.$0is still a bit unpredictable, as functions invoked as$fn-Xdo not get$0set toX. But within the body of a function, you're at least now ~guaranteed to have$0set to the function's name, the script you're running, or the es binary.QUESTION: some hook functions are not invoked in a way where
$0is set to the function name.Is this unexpected? Should we scan through these cases and replace
varlookups withfnlookups? It is inconsistent that the syntax-expanded hook functions like%backquoteget$0set to the function name but shell-lookup hook functions like%homedon't.