Skip to content

--function / -f option for set command - #3465

Closed
chamini2 wants to merge 6 commits into
fish-shell:masterfrom
chamini2:set_function
Closed

chamini2 wants to merge 6 commits into
fish-shell:masterfrom
chamini2:set_function

Conversation

@chamini2

@chamini2 chamini2 commented Oct 16, 2016 •

Copy link
Copy Markdown

Related to #565 (last comment).

@faho faho added this to the next-2.x milestone Oct 16, 2016
@krader1961

Copy link
Copy Markdown
Contributor

I reviewed this and played with it a bit. Looks good. The only possible issue is that set -f outside function scope is effectively equivalent to set -g. Is that the behavior we want or should it be an error?

We also need more comprehensive unit tests for setting vars but that's probably better handled in a separate change. Thanks for adding a basic test.

Finally, is this really necessary? You can get the same result today by just doing set -l at the top of the function and doing plain set without the -l in nested blocks within the function. This change does make it easier to use function scope vars (just set -f throughout the function) but is that worth adding another flag?

@chamini2

Copy link
Copy Markdown
Author

Well, this was discussed thoroughly in #565 and I was interested in this case because I had some trouble with it myself (#3323). In my opinion, the main reason to include this flag is:

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.

Also, the example given in this comment shows how useful it can be to set function variables in a list.

@chamini2

Copy link
Copy Markdown
Author

The only possible issue is that set -f outside function scope is effectively equivalent to set -g. Is that the behavior we want or should it be an error?

I thought the ideal solution would be to behave as a set with no flag that creates a new variable, so there's no surprises. Although it also makes sense to throw an error in case it's used in the global scope.

@krader1961

Copy link
Copy Markdown
Contributor

Well, this was discussed thoroughly in #565

Yes, but the arguments near the end to add a flag seemed weak. The only thing universally agreed to in that issue is that the scoping rules should be changed and set varname varvalue inside a function should be implicitly function scoped. But doing so can only be done with the next major release.

Also, the example given in this comment...

Okay, that's at least a concrete example that shows how set -f avoids needing to insert several set -l statements at the top of the function. It's an argument, but I think a weak one. Given that you've already created the PR I have no objection to merging it after we decide if using set -f outside a function should throw an error. But had I known you were going to do this I would have asked for more discussion about whether adding set -f just to avoid the need to set -l at the top of the function (or at least outside any inner blocks) justified the effort.

@chamini2

Copy link
Copy Markdown
Author

I understand that these arguments don't seem strong enough to you, a single use case isn't enough reason to add a flag. But I insist that the main reason for this is consistency, if a scoping rule is present somehow (in this case, only when a variable doesn't already exist in any scope) it should also be allowed to be used at will of the user (read: with a flag).

@faho

faho commented Oct 21, 2016

Copy link
Copy Markdown
Member

@chamini2: First of all, thanks for your work.

There is some weirdness in set --query behavior here:

function t
    set -qg PATH; and echo "PATH is global"
    set -qf PATH; and echo "PATH is function-scoped"
    set -ql PATH; and echo "PATH is local"
    set -f foo bar
    set -qf foo; and echo "foo is function-scoped"
    set -ql foo; and echo "foo is local"
    set -qg foo; and echo "foo is global"
end

here, $PATH is both global and function-scoped, while $foo is both local and function-scoped, at least as far as set -q is concerned.

The latter might be okay - it's defined in the current block after all, but the former is just plain weird.

Now, personally, I'd rather allow something like set -qfl foo to return true if foo is either function-scoped or local (i.e. as a shortcut for set -qf foo; or set -ql foo).

Also, I'd assume that people would make the connection more between function-scoped and local (at least I did, and I like assuming that people are like me), so it's probably less confusing if set -f outside of a function makes that variable local. Then the definition of function-scope is essentially "define the variable in- and outside of the current block". Global variables should be something you explicitly request, not something that happens on accident.

Either that or throw an error - you know if you're in a function or not. However, I think we want to encourage using "-f", so if we can make it "safe", so that people just use it when they don't need anything special, that'd be great (mkay). Erroring out means people need to think about using it, which means they won't as much - people don't like to think (see above).

@krader1961: I know I've wanted this. One example is using set -q for boolean values:

function foo
    if bar # where bar is a complicated condition that can't just be inlined later
       set -f baz
    end
    if set -qf baz
        # Yay, the test before succeeded!
    else
        # Oh noes, bar is closed!
    end
end

@krader1961

Copy link
Copy Markdown
Contributor

I think your example, @faho, is a bit contrived but I do agree, upon further reflection, that we want an explicit flag so that

a) we gain some experience with how things will work if we make it the default behavior, and

b) until such time as we can make it the default there is an easy way for people to get that behavior without having to remember that set -l inside an inner block of a function will shadow the set -l at the top of the function when what they really want is set -f behavior at both locations.

My testing verified that the variable had the expected value for various combinations of set flags and functions with/without --no-scope-shadowing. I did not test set -q. I suspect the weird behavior is due to the same reason that you can do set -f outside of function scope with this change. I think both of those behaviors should be changed.

@chamini2

Copy link
Copy Markdown
Author

Ok, read your comments and I will make it so set -f behaves like set -l outside of a function, I prefer to not throw an error. That would be the desired change, right?

@chamini2

Copy link
Copy Markdown
Author

Hey, I pushed some more tests, please review them and let me know if they reflect the expected behaviour. The tests don't pass yet, but I want to make sure this is what we should get before actually working on it.

@krader1961

Copy link
Copy Markdown
Contributor

make it so set -f behaves like set -l outside of a function

I'm ambivalent. I agree that throwing errors is not friendly. On the other hand letting people shoot themselves in the foot is also not friendly. The -l and -g flags have well defined, fairly obvious, meanings outside of a function block. The same is not true for -f. Also, what does it mean to do set -qf outside of a function? Will it return all locally scoped vars? And if so then it's not consistent with the behavior we expect inside a function. And if not then why would should set -f be equivalent to set -l outside of a function body?

I'm inclined to say that before merging this, or any similar, change we should have a discussion in the original issue that resolves all these questions (including those earlier today on Gitter). Preferably via concrete code examples that enumerate the various permutations and the expected output (which then become the basis for unit tests).

@chamini2

Copy link
Copy Markdown
Author

Completely agree.

@krader1961

Copy link
Copy Markdown
Contributor

I'm going to close this since there are fundamental questions about the behavior that need to be answered in issue #565. Thank you very much, @chamini2, for providing a concrete implementation to drive the discussion forward. But it's pretty clear that this tentative implementation won't be merged. I'm hoping we can reach consensus in issue #565 regarding the unresolved questions and you'll create a new PR that addresses them.

@zanchey zanchey removed this from the next-2.x milestone Nov 8, 2016
@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.

4 participants