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
set command should offer function scope as a flag too #3323
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.
$ 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
$ functiont# f for "function scope"set -f var1 "won't show"set -f var2 "will show"
end
$ t
$ echo$var1
$ echo$var2
previous value
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.
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
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.
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
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.
@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.
Activity
chamini2 commentedon Aug 22, 2016
An example
With the flag
krader1961 commentedon Aug 23, 2016
It's not clear how your proposed
-fflag differs in behavior from-l. If you modify your second example to useset -ldo 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.chamini2 commentedon Aug 23, 2016
You're right, this example doesn't show the problem well, I'll make a more complete one:
krader1961 commentedon Aug 23, 2016
That example can be solved by simply adding these statements to the top of the function:
It's not clear to me that implementing
set -fadds 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 -fmight be justified but this is going to require a lot of discussion.chamini2 commentedon Aug 23, 2016
The script I wrote is a smaller version of the real one, I pass
So for doing
I could just do
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.
faho commentedon Aug 23, 2016
This has also been discussed in #565, so let's close this as a duplicate.
chamini2 commentedon Aug 23, 2016
@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 thefunciton scopeeven if global vars are present, so they don't get modified even if they exist.faho commentedon Aug 23, 2016
@chamini2: Read on, that was the initial proposal, I proposed almost exactly what you are asking for here later on in the thread.