Skip to content

implement "tied" variables - #4082

Closed
krader1961 wants to merge 3 commits into
fish-shell:masterfrom
krader1961:tied-vars
Closed

krader1961 wants to merge 3 commits into
fish-shell:masterfrom
krader1961:tied-vars

Conversation

@krader1961

@krader1961 krader1961 commented May 31, 2017 •

Copy link
Copy Markdown
Contributor

People have wanted a way to more transparently handle other env vars
that are implicitly lists much like fish already does for PATH, MANPATH,
and CDPATH. I was originally leaning towards augmenting the set builtin
to allow the user to mark other vars for the same magic treatment. But
after much thought I decided the zsh "tied" variable feature is more
flexible and easier to use. This change implements that for fish.

Fixes #436

TODOs:

The documentation needs more work. I also need to add more unit tests to verify corner cases. But the core implementation seems solid so I wanted to get this out for discussion.

I am ambivalent about whether to introduce a new tie command or augment set with -T / --tie and -U / --untie flags. Thoughts?

@krader1961

Copy link
Copy Markdown
Contributor Author

Not too surprisingly it turns out that doing this entirely in fish script has one slightly annoying drawback. When setting one of the tied variables local to a block (i.e., set -l) you have to also set its companion to be a local var for that block. I've noted this in the documentation. Whether this is merely annoying or a real problem is debatable. I think we can live with that limitation.

Note that zsh has a similar limitation but its inverse:

macpro% typeset -T XX xx
macpro% XX=a:b:c
macpro% echo $xx
a b c
macpro% function x {
function> local XX=d:e:f
function> echo XX=$XX
function> echo xx=$xx
function> }
macpro% echo $xx
a b c
macpro% echo $XX
a:b:c
macpro% x
XX=d:e:f
xx=a b c
macpro% echo $XX
a:b:c
macpro% echo $xx
a b c

In zsh creating a local version of one of the tied vars does not affect its mirror in that block. On the other hand when you exit the block both are back in sync. I haven't done more testing but I think the behavior this change introduces is somewhat more obvious.

@krader1961

Copy link
Copy Markdown
Contributor Author

Also, regarding set -l we could make it transparent and not require the user to do set -l on both the scalar and array var in a tied pair. This would require teaching the set builtin about the private data structures (i.e., the __fish_tied_* vars) that define tied vars.

@zanchey

zanchey commented Jun 1, 2017

Copy link
Copy Markdown
Member

tie as a command name is already in use by TeX on my systems, although that's not an absolute reason not to use it for a builtin.

Tied variables seem like an Extremely Magic feature! Is there some way we can warn people if they're setting a tied variable?

@krader1961

Copy link
Copy Markdown
Contributor Author

tie as a command name is already in use by TeX on my systems...

I'm okay with making this a behavior of the set command rather than introducing a new command. Although I do have a slight preference for a distinct command. I think it makes the behavior clearer, easier to document, and slightly less likely to result in mistakes.

Is there some way we can warn people if they're setting a tied variable?

Sure, but such messages tend to be annoying. We'd obviously want to limit how often they're issued. Just once the first time a user changes any tied variable? Just once when they attempt to change one of the six auto-created tied variables? Or once for each tied variable? We could also document a global or universal var the user could set to inhibit those messages.

@krader1961

Copy link
Copy Markdown
Contributor Author

FYI, I decided that requiring the user to deal with set -l on tied variables was too likely to result in mistakes. So I went ahead and added the necessary code to the core C++ code. I also updated the unit tests and man page.

Kurtis Rader added 3 commits June 8, 2017 17:55
People have wanted a way to more transparently handle other env vars
that are implicitly lists much like fish already does for PATH, MANPATH,
and CDPATH. I was originally leaning towards augmenting the set builtin
to allow the user to mark other vars for the same magic treatment. But
after much thought I decided the zsh "tied" variable feature is more
flexible and easier to user. This change implements that for fish.

Fixes #436
I was hoping to keep the entire tied var implemention in fish script.
Unfortunately there isn't any way to do so -- at least not
transparently. This change makes it transparent.

Fixes #436
The `man` function happened to define a variable named `fish_manpath`
which conflicts with the newly introduced tied array var by that name.
@krader1961

Copy link
Copy Markdown
Contributor Author

At this point in time I'm primarily interested in whether anyone has an objection to introducing a tie command as a means of addressing issue #436. I'm less concerned with getting feedback on the code. Does the approach seem reasonable? Is the man page sufficiently clear? Would you prefer to fold this into the set command rather than introduce a new command? For example, by adding a set --tie flag.

I'm going to let this soak for a couple more days and if no one has objections merge it.

@zanchey

zanchey commented Jun 13, 2017

Copy link
Copy Markdown
Member

I'd stick with set --tie.

@krader1961

Copy link
Copy Markdown
Contributor Author

Okay, a couple of people have expressed a desire to use set --tie rather than introduce a tie command. I have minor misgivings given that set --tie won't actually define the variables let alone let you assign a value at the time the tie is created. This seems likely to be a source of mistakes in its use. On the other hand this operation is intimately related to setting variables and we should avoid creating new commands when possible. Anyone else have an opinion?

Does anyone have an opinion on making PATH be tied to fish_path, MANPATH to fish_manpath, and CDPATH to fish_cdpath? Once we establish these names it won't be possible to change them. We'll want to start converting scripts to using the new array names so that at a future date (e.g., fish 3.0) we can stop auto-splitting/joining those scalar env vars.

@krader1961

Copy link
Copy Markdown
Contributor Author

No one else has an opinion? If no one else says anything in the next couple of days I'm going to close this PR and reimplement this feature in terms of set --tie rather than a tie command. The functionality will otherwise be identical to what is in this PR.

@faho

faho commented Jun 19, 2017

Copy link
Copy Markdown
Member

No one else has an opinion?

Honestly, I still don't quite get the feature. I can see how it'd be cleaner for $PATH et al since those aren't special-cased anymore, but I don't think I'd ever use it. I'd rather string split explicitly.

I do have a slight preference for set --tie - this is related to creating variables, and we want to be careful with introducing more builtins.

@krader1961

Copy link
Copy Markdown
Contributor Author

@faho: This means that all the other path like env vars people work with (e.g., GOPATH, LD_LIBRARY_PATH) are easier to work with. You can treat them as a traditional scalar or a fish array without having to remember to convert between the representations. And, at some future date, we can tell people that PATH no longer has its current, surprising, behavior. If someone wants to manipulate it by doing set PATH /my_dir:$PATH like they would in sh/bash/zsh they can do so without getting unexpected results. Or they can manipulate its array analog fish_path in a more fishy manner. But, again, without any other code having to worry about adding otherwise superfluous string split : and string join : commands.

@JoshCheek

Copy link
Copy Markdown

Seems like an interesting feature. I like that you can specify the separator. I'm not a fan of the tied var implicitly changing which variable it is tied to based on the scope it's accessed from (I like Zsh's behaviour).

@krader1961

Copy link
Copy Markdown
Contributor Author

I'm not a fan of the tied var implicitly changing which variable it is tied to based on the scope it's accessed from.

I don't understand. It doesn't change which var it is tied to. However, if you localize one of them it does automatically localize the other. That is what most people will expect. When I saw how zsh handled localizing a tied variable I was quite surprised at the result.

@JoshCheek

Copy link
Copy Markdown
set -l gopath /path3 /path4

From here, I assumed that this created a new variable within the block's scope. Like JavaScript's var. I would assume that there are now two gopath variables, one global and one scoped to that block, and I would assume they have nothing to do with each other. I thought that was the point of the --local option, so that you don't have to worry about accidentally shadowing variables in an outer context.

@krader1961

Copy link
Copy Markdown
Contributor Author

@JoshCheek: That's how the implementation works. If you run the following script with a fish built with this PR you'll see this output:

== global scope
GOPATH /tmp:/usr/local
gopath /tmp /usr/local
== local scope
GOPATH /Users/krader/tmp:/Users/krader/bin
gopath /Users/krader/tmp /Users/krader/bin
== back to global scope
GOPATH /tmp:/usr/local
gopath /tmp /usr/local

If you're expecting something else I'm confused what behavior you're expecting.

Here is the script:

tie GOPATH
set -gx GOPATH /tmp:/usr/local
echo == global scope
echo GOPATH $GOPATH
echo gopath $gopath
begin
    echo == local scope
    set -l gopath $HOME/tmp $HOME/bin
    echo GOPATH $GOPATH
    echo gopath $gopath
end
echo == back to global scope
echo GOPATH $GOPATH
echo gopath $gopath

@szhu

szhu commented Jun 21, 2017

Copy link
Copy Markdown

Do you think this behavior may conflict with scripts that assume that variables can be changed independently? (Imagine a script that naively backs up/restores all the global variables that cannot restore the original variable values after two variables are tied together.)

@krader1961

Copy link
Copy Markdown
Contributor Author

@szhu: Yes, this is definitely not a zero risk change. Same is true for the change in behavior of set -lx that @faho has out for review. That's why I've opened issue #4154 to discuss whether it is time to focus on a few changes of this nature for a fish 3.0 release rather than risk including them in a minor release.

@faho faho modified the milestones: fish-3.0, fish 2.7.0 Jun 22, 2017
@floam

floam commented Sep 25, 2017

Copy link
Copy Markdown
Member

Why was this closed? Tied variables would be cool for 3.0.

@floam

floam commented Sep 25, 2017 •

Copy link
Copy Markdown
Member

Nevermind, I see the set --tie preference.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

special-casing PATH/MANPATH/CDPATH is weird; we need a more general solution like zsh "tied" variables

6 participants