Skip to content

Commit

Permalink
Initialize a command var to $nop~
Browse files Browse the repository at this point in the history
You cannot assign $nil to a command var so that should not be the initial
value of `var x~`. Instead, assign $builtin:nop~.

Fixes elves#1248
  • Loading branch information
krader1961 committed Mar 29, 2021
1 parent 29034c2 commit e2759f7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions 0.16.0-release-notes.md
Expand Up @@ -11,6 +11,10 @@ This is the draft release notes for 0.16.0. It is scheduled to be released on
Instead, the history listing mode now applies smart-case matching by
default.

- Declaring a variable with a `~` suffix, without an explicit initial value,
now defaults to $builtin:nop rather than $builtin:nil
([#1248](https://b.elv.sh/1248)).

# Notable bugfixes

- The `path:is-dir` and `path:is-regular` commands no longer follow symlinks,
Expand Down
3 changes: 3 additions & 0 deletions pkg/eval/builtin_special_test.go
Expand Up @@ -24,6 +24,9 @@ func TestVar(t *testing.T) {
That("var 'a/b'", "put $'a/b'").Puts(nil),
// Declaring one variable whose name ends in ":".
That("var a:").DoesNothing(),
// Declaring a variable whose name ends in "~" has default value $builtin:nop rather than
// $builtin:nil. See https://github.com/elves/elvish/issues/1248.
That("var cmd~; cmd &ignored-opt ignored-arg").DoesNothing(),
// Declaring multiple variables
That("var x y", "put $x $y").Puts(nil, nil),
// Declaring one variable with initial value
Expand Down
2 changes: 1 addition & 1 deletion pkg/eval/closure.go
Expand Up @@ -146,7 +146,7 @@ func (c *closure) Call(fm *Frame, args []interface{}, opts map[string]interface{
func MakeVarFromName(name string) vars.Var {
switch {
case strings.HasSuffix(name, FnSuffix):
val := Callable(nil)
val := NewGoFn("nop~", nop)
return vars.FromPtr(&val)
case strings.HasSuffix(name, NsSuffix):
val := (*Ns)(nil)
Expand Down
3 changes: 2 additions & 1 deletion website/ref/language.md
Expand Up @@ -621,7 +621,8 @@ when used as the suffix of a variable name:

- If a variable name ends with `~`, it can only take callable values, which
are functions and external commands. Such variables are consulted when
resolving [ordinary commands](#ordinary-command).
resolving [ordinary commands](#ordinary-command). The default value is the
[`builtin:nop`](builtin.html#nop) command.

- If a variable name ends with `:`, it can only take namespaces as values.
They are used for accessing namespaced variables.
Expand Down

0 comments on commit e2759f7

Please sign in to comment.