You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
enhance set with a flag to force updating the enclosing scope (augments function --no-scope-shadowing) #3540
When using the -S parameter for functions to disabled scope-shadowing, naming conflicts can occur. As a more robust alternative, a mechanism like tcl's upvar would be useful, presumably implemented as option to the set builtin.
Reproduction steps
Create the following fish script file:
#!/usr/bin/env fish
function append -S -a var item
set $var $$var $item
end
set x xx; append x xy
echo "Expecting x='xx xy', got x='$x'"
set var xx; append var xy
echo "Expecting var='xx xy', got var='$var'"
set item xx; append item xy
echo "Expecting item='xx xy', got item='$item'"
Just to be clear for anyone else puzzled by what's going on. This is only a problem if you use var indirection (e.g., set $var new-value) and the value of $var is the same as the name of a local or function scope var. Which is true in the example provided by the O.P.
Personally I'm inclined to agree that an explicit set flag to mean "update the enclosing scope" is both clearer, safer, and more robust than the function --scope-shadowing mechanism.
Clarification: the function --no-scope-shadowing actually does two distinct things. A reference to $var references the value set in the enclosing function (rather than the global scope) if it hasn't already been set with local scope. If you do set it without the -l flag then it modifies the value in the enclosing function rather than the global scope. This proposal would only affect the second behavior. Frankly, this is a bit of a mess and needs a lot of discussion before we make any changes.
Another use case for this feature: a set function that wraps the builtin. I needed to create such a function to facilitate implementing tied variables (issue #4082). But I can't do that because set -l doesn't create a function visible to the calling function.
Reacted by Braham Snyder and Beni Cherniavsky-Paskin
Activity
krader1961 commentedon Nov 11, 2016
Just to be clear for anyone else puzzled by what's going on. This is only a problem if you use var indirection (e.g.,
set $var new-value) and the value of$varis the same as the name of a local or function scope var. Which is true in the example provided by the O.P.Personally I'm inclined to agree that an explicit
setflag to mean "update the enclosing scope" is both clearer, safer, and more robust than thefunction --scope-shadowingmechanism.krader1961 commentedon Nov 11, 2016
Clarification: the
function --no-scope-shadowingactually does two distinct things. A reference to$varreferences the value set in the enclosing function (rather than the global scope) if it hasn't already been set with local scope. If you do set it without the-lflag then it modifies the value in the enclosing function rather than the global scope. This proposal would only affect the second behavior. Frankly, this is a bit of a mess and needs a lot of discussion before we make any changes.kbauer commentedon Nov 11, 2016
Related: #1046
krader1961 commentedon Jul 22, 2017
Another use case for this feature: a
setfunction that wraps the builtin. I needed to create such a function to facilitate implementing tied variables (issue #4082). But I can't do that becauseset -ldoesn't create a function visible to the calling function.