Skip to content

set command should offer function scope as a flag too #3323

Closed

Description

@chamini2

In the documentation of set it states:

If a variable is not explicitly set to be either universal, global or local and has never before been defined, the variable will be local to the currently executing function. Note that this is different
from using the -l or --local flag. If one of those flags is used, the variable will be local to the most inner currently executing block, while without these the variable will be local to the function.
If no function is executing, the variable will be global.

This is a good option to write functions, but if there's a globally set variable with the same name as one used in the function, it will overwrite it, there should be an option to make a var scoped to the current function, that way we avoid modifying globally set vars.

Activity

  1. chamini2 commented on Aug 22, 2016

    @chamini2
    Author

    An example

    $ set var2 "previous value"
    $ echo $var2
    previous value
    $ function t 
        set var1 "won't show"
        set var2 "will show"
      end
    $ t
    $ echo $var1
    
    $ echo $var2
    will show

    With the flag

    $ set var2 "previous value"
    $ echo $var2
    previous value
    $ function t 
        # f for "function scope"
        set -f var1 "won't show"
        set -f var2 "will show"
      end
    $ t
    $ echo $var1
    
    $ echo $var2
    previous value
  2. krader1961 commented on Aug 23, 2016

    @krader1961
    Contributor

    It's not clear how your proposed -f flag differs in behavior from -l. If you modify your second example to use set -l do you not see the behavior you desire? I do but then I'm not sure I understand what you're proposing. Note that a function is a "block" exactly like a "if" or "for" block.

  3. chamini2 commented on Aug 23, 2016

    @chamini2
    Author

    You're right, this example doesn't show the problem well, I'll make a more complete one:

    $ function t
      for tag in ARTIST ALBUM
        set $tag (metaflac "06 - This Game.flac" --show-tag=$tag | string replace "$tag=" "")
      end
      flac -c -d "06 - This Game.flac" |\
        lame -h --quiet -b 320 --add-id3v2 --pad-id3v2 --ignore-tag-errors\
          --ta "$ARTIST" --tl "$ALBUM" - "06 - This Game.mp3"
    end
    
    $ set ARTIST "The Glitch Mob"
    $ t
    06 - This Game.flac: done
    $ echo $ARTIST
    Parov Stelar
  4. krader1961 commented on Aug 23, 2016

    @krader1961
    Contributor

    That example can be solved by simply adding these statements to the top of the function:

    set -l ARTIST
    set -l ALBUM
    

    It's not clear to me that implementing set -f adds enough value in that example to make the added implementation complexity worthwhile. That's not to say the idea is wholly without merit. Only that your argument so far is not convincing.

    I'm actually partial to the Python scoping rules which would give you the semantics you desire. But it's far too late in the development of fish to make such a radical change in behavior. At least not without making a new major release that explicitly breaks compatibility with existing scripts. Something that is unlikely to happen. In light of that adding a set -f might be justified but this is going to require a lot of discussion.

  5. chamini2 commented on Aug 23, 2016

    @chamini2
    Author

    The script I wrote is a smaller version of the real one, I pass

    for tag in ARTIST TITLE ALBUM GENRE TRACKNUMBER DATE
      set $tag (metaflac $song --show-tag=$tag | string replace "$tag=" "")
    end

    So for doing

    set -l ARTIST
    set -l TITLE
    set -l ALBUM
    set -l GENRE
    set -l TRACKNUMBER
    set -l DATE
    for tag in ARTIST TITLE ALBUM GENRE TRACKNUMBER DATE
      set $tag (metaflac $song --show-tag=$tag | string replace "$tag=" "")
    end

    I could just do

    set -l ARTIST      (metaflac $song --show-tag=ARTIST      | string replace "ARTIST="      "")
    set -l TITLE       (metaflac $song --show-tag=TITLE       | string replace "TITLE="       "")
    set -l ALBUM       (metaflac $song --show-tag=ALBUM       | string replace "ALBUM="       "")
    set -l GENRE       (metaflac $song --show-tag=GENRE       | string replace "GENRE="       "")
    set -l TRACKNUMBER (metaflac $song --show-tag=TRACKNUMBER | string replace "TRACKNUMBER=" "")
    set -l DATE        (metaflac $song --show-tag=DATE        | string replace "DATE="        "")

    And be done with it.


    But apart from that inconvenience, it makes more sense to me that if there's a default scope, it should be also offered as an option in the flags. It seems weird that we get half the behaviour: looking for vars in each scope and if none were found create a new one scoped to the function introduces the new idea of "function scoped variables" but then doesn't allow for fully using that scoping style through a flag.

  6. faho commented on Aug 23, 2016

    @faho
    Member

    This has also been discussed in #565, so let's close this as a duplicate.

  7. removed this from the milestone on Aug 23, 2016
  8. chamini2 commented on Aug 23, 2016

    @chamini2
    Author

    @faho, sorry if I'm misunderstanding, but #565 seems to discuss the default behaviour of set, I don't mean to change the default but to enable a flag (-f) to enforce the funciton scope even if global vars are present, so they don't get modified even if they exist.

  9. faho commented on Aug 23, 2016

    @faho
    Member

    @chamini2: Read on, that was the initial proposal, I proposed almost exactly what you are asking for here later on in the thread.

  10. locked as resolved and limited conversation to collaborators on Apr 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions