Skip to content

enhance set with a flag to force updating the enclosing scope (augments function --no-scope-shadowing) #3540

Open

Description

@kbauer

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'"

and run it. The output will be

Expecting x='xx xy', got x='xx xy'
Expecting var='xx xy', got var='xx'
Expecting item='xx xy', got item='xx'

  • Have you checked if problem occurs with fish 2.4.0?
  • Tried fish without third-party customizations (check sh -c 'env HOME=$(mktemp -d) fish')?

fish version installed (fish --version): fish, version 2.4.0-111-g7356987

OS/terminal used: Windows 10, mintty

Activity

  1. krader1961 commented on Nov 11, 2016

    @krader1961
    Contributor

    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.

  2. krader1961 commented on Nov 11, 2016

    @krader1961
    Contributor

    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.

  3. kbauer commented on Nov 11, 2016

    @kbauer
    Author

    Related: #1046

  4. krader1961 commented on Jul 22, 2017

    @krader1961
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions