-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
For-loops and variable change events #8384
Comments
Yeah I think this is a bug. I expect to get events only when a global/universal variable is changed. |
I would expect local variables not to post these events. |
Turning off variable handlers for local variables is a possibility. Do we have any idea if someone's trying to use that? The only idea I can come up with is to have a thing run simply more often. |
Actually I'm not sure if I was right to suggest local variables should not fire events. There are probably valid use cases where a global variable has an on-variable-change handler, and a person would expect it to still work even if they make a local copy for the sake of not messing things up, are about to export it or something, etc. |
Okay, please look at one of our fine, artisinal code samples: > function foo --on-variable foo; echo $argv Value of foo: $foo; end
> set -g foo global
VARIABLE SET foo Value of foo: global
> set -l foo local1
VARIABLE SET foo Value of foo: global
> set foo local2
VARIABLE SET foo Value of foo: global
> set foo local3
VARIABLE SET foo Value of foo: global
> set foo local4
VARIABLE SET foo Value of foo: global
# ... As you see, the variable handler is triggered, but there's no way to tell that anything happened. As far as it is concerned, what happened was So there is no way to react to the actual value of the variable, so I do not see what use the variable handler has. Even if you e.g. said that it should show that the variable was shadowed, there is
So, what I would like here is a concrete example of how you would use this for anything. |
function foo_watcher --no-scope-shadowing --on-variable foo;
echo "$argv="
set --show foo
end
> set -g foo bar
VARIABLE SET foo=
$foo: set in global scope, unexported, with 1 elements
$foo[1]: |bar|=
> set -l foo MOOCOW
VARIABLE SET foo=
$foo: set in local scope, unexported, with 1 elements
$foo[1]: |MOOCOW|
$foo: set in global scope, unexported, with 1 elements
$foo[1]: |bar|
> set foo jar
VARIABLE SET foo=
$foo: set in local scope, unexported, with 1 elements
$foo[1]: |jar|
$foo: set in global scope, unexported, with 1 elements
$foo[1]: |bar|
$foo: set in univ |
That's --no-scope-shadowing, which is cheating by definition, and I don't believe something we have to bend over backwards to support. |
which is cheating by definition, and I don't believe something we have to bend over backwards to support. It works right now, doesn't it? |
Let me put it this way: If we break that, and make everything else better, that's a good deal. "--no-scope-shadowing" is a hack made to support something like psub, but without actually implementing the needed things, or something. (edit: first mention I can find is 88efc73, implemented for My question remains: What do you need this for? Why would you need this handler? What's the use case? A concrete example, not an abstract "look ma, no handlebars" please. Because if there is no real use, we can simply document "variable handlers are only triggered for global and universal variables" and be done. If there's no real need, there's no real breakage, so it's all fine. |
I'm not really sure how |
Okay, let's not get bogged down into "is it cheating or not". There are two questions here:
The second question here asks "useful". It's not about if you can theoretically finagle something together with "--no-scope-shadowing" and a prayer, it's about actual use. I have no problem simpy adding a note here that variable handlers only work for global/universal variables if there's no actual use in doing it for locals, especially because that requires --no-scope-shadowing, so it looks broken. |
Okay, so a realistic example could be There are a lot of things a person might do with a monitored variable in any scope. |
Yeah, that makes sense. Okay, think I'm gonna remove the duplicate event handler for for-loops and call it a day. |
I don't think that example works. set -g user_paths /global/bin
function __fish_reconstruct_path --on-variable user_paths
echo adding $user_paths to \$PATH
# ...
end
begin
set -l user_paths /local/bin
# run some command assuming that /local/bin is in $PATH - but it's not!
end The problem is that __fish_reconstruct_path only receives the global value. So I don't think events on locals are useful. |
Not if you use We currently don't, but it is something at least something semi-reasonable you might want to do. |
My bad, I missed that. Ok then it's probably reasonable to keep, time will tell if it's useful in practise. Removing the event for |
Pretty sure the for-loop thing existed since #4376, i.e. fish 3.0. |
While working on reducing the overhead of running for-loops, I stumbled upon one weird fact:
See how that calls the event handler four times? That's because this called parser::set_var_and_fire to set the variable initially, which pins it to the outer scope.
That's probably useless.
However, I'm not sure the events here have a use in general. The reason is that for sets the variable as a local to the enclosing block, so no variable handler would ever be able to touch it?
Do we want to just remove the superfluous handler or the event firing here in general?
(or do we even want to abolish events for local variables entirely?)
Is there a use here I'm not seeing?
The text was updated successfully, but these errors were encountered: