fish-shell/doc_src
Fabian Homborg 733114fefb
Add set --function (#8145)
* Add `set --function`

This makes the function's scope available, even inside of blocks. Outside of blocks it's the toplevel local scope.

This removes the need to declare variables locally before use, and will probably end up being the main way variables get set.

E.g.:

```fish
set -l thing
if condition
    set thing one
else
    set thing two
end
```

could be written as

```fish
if condition
    set -f thing one
else
    set -f thing two
end
```

Note: Many scripts shipped with fish use workarounds like `and`/`or`
instead of `if`, so it isn't easy to find good examples.

Also, if there isn't an else-branch in that above, just with

```fish
if condition
    set -f thing one
end
```

that means something different from setting it before! Now, if
`condition` isn't true, it would use a global (or universal) variable of
te same name!

Some more interesting parts:

Because it *is* a local scope, setting a variable `-f` and
`-l` in the toplevel of a function ends up the same:

```fish
function foo2
    set -l foo bar
    set -f foo baz # modifies the *same* variable!
end
```

but setting it locally inside a block creates a new local variable
that shadows the function-scoped variable:

```fish
function foo3
    set -f foo bar
    begin
        set -l foo banana
        # $foo is banana
    end
    # $foo is bar again
end
```

This is how local variables already work. "Local" is actually "block-scoped".

Also `set --show` will only show the closest local scope, so it won't
show a shadowed function-level variable. Again, this is how local
variables already work, and could be done as a separate change.

As a fun tidbit, functions with --no-scope-shadowing can now use this to set variables in the calling function. That's probably okay given that it's already an escape hatch (but to be clear: if it turns out to problematic I reserve the right to remove it).

Fixes #565
2021-08-01 20:08:12 +02:00
..
cmds Add set --function (#8145) 2021-08-01 20:08:12 +02:00
python_docs_theme docs: Remove background from pygments 2021-07-30 18:36:12 +02:00
commands.rst We no longer have two doc systems, move sphinx_doc_src back to doc_src 2020-02-19 17:00:35 -08:00
completions.rst docs: Replace all internal links with :ref:s 2021-03-26 19:32:14 +01:00
conf.py docs: Remove lAtEx thing again 2021-07-14 17:03:41 +02:00
design.rst Make rationale spacing consistent 2020-10-12 20:25:35 +02:00
faq.rst docs: Made the abort/edit history feature more discoverable. 2021-07-03 16:39:32 +02:00
fish_for_bash_users.rst docs: Fix a label 2021-07-27 18:49:34 +02:00
fish_indent_lexer.py Highlight keywords differently 2021-02-07 21:18:51 +01:00
index.rst doc: Some more rewordings 2021-07-23 18:00:57 +02:00
interactive.rst docs: Document that commands with space will be kept until the next 2021-08-01 14:01:49 +02:00
language.rst Add set --function (#8145) 2021-08-01 20:08:12 +02:00
license.rst Update copyright years 2021-05-03 13:12:11 +02:00
relnotes.rst docs: call the CHANGELOG "release notes" 2021-02-25 22:46:24 +08:00
tutorial.rst docs: Add some links 2021-07-16 18:08:55 +02:00