Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch 8.2.2744: Vim9: no way to explicitly ignore an argument
Problem: Vim9: no way to explicitly ignore an argument. Solution: Use the underscore as the name for an ignored argument.
- Loading branch information
Showing
7 changed files
with
86 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2619,6 +2619,41 @@ def Test_compile_error() | |
delfunc g:Broken | ||
enddef | ||
|
||
def Test_ignored_argument() | ||
var lines =<< trim END | ||
vim9script | ||
def Ignore(_, _): string | ||
return 'yes' | ||
enddef | ||
assert_equal('yes', Ignore(1, 2)) | ||
This comment has been minimized.
Sorry, something went wrong.
lacygoill
|
||
|
||
func Ok(_) | ||
return a:_ | ||
endfunc | ||
assert_equal('ok', Ok('ok')) | ||
|
||
func Oktoo() | ||
let _ = 'too' | ||
return _ | ||
endfunc | ||
assert_equal('too', Oktoo()) | ||
END | ||
CheckScriptSuccess(lines) | ||
|
||
lines =<< trim END | ||
def Ignore(_: string): string | ||
return _ | ||
enddef | ||
defcompile | ||
END | ||
CheckScriptFailure(lines, 'E1181:', 1) | ||
|
||
lines =<< trim END | ||
var _ = 1 | ||
END | ||
CheckDefAndScriptFailure(lines, 'E1181:', 1) | ||
enddef | ||
|
||
|
||
|
||
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit 962c43b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If Vim is more strict regarding the arguments specified in a lambda, shouldn't this snippet raise an error?
vim9script
echo [0, 1, 2]->map(() => 123)
[123, 123, 123]
It does not, even though:
- the lambda is passed the index of each item in the list, as well as the item itself
- we didn't specify any argument in the lambda
Shouldn't we be forced to explicitly tell Vim about those arguments?
echo [0, 1, 2]->map((_, v) => 123)
^--^
Just like we now have to explicitly tell Vim that a timer's callback is expected to receive its id as an argument:
vim9script
timer_start(0, () => 123)
E118: Too many arguments for function: <lambda>1
vim9script
timer_start(0, (_) => 123)
no error
Line 189 in
doc/vim9.txt
:It doesn't work in a lambda passed to
map()
:Tested on 8.2.2744.