Allow default expressions for named arguments#3952
Allow default expressions for named arguments#3952andymass wants to merge 5 commits intovim:masterfrom
Conversation
e.g., func Func(mandatory, optional = 10 + 20) The expressions are evaluated each time at call time, like ruby. So it is possible to use invalid expressions or those with side-effects.
free memory, show defaults in :function<cr>
Codecov Report
@@ Coverage Diff @@
## master #3952 +/- ##
==========================================
+ Coverage 78.93% 79.14% +0.21%
==========================================
Files 105 105
Lines 142155 141168 -987
==========================================
- Hits 112204 111727 -477
+ Misses 29951 29441 -510
Continue to review full report at Codecov.
|
|
Interesting. Do you have plan to suppot keyword arguments? |
|
Bram's comment: https://groups.google.com/d/msg/vim_dev/5p_6AFwBB9c/GHQJqo_XAwAJ |
|
@mattn maybe, but it is more difficult to implement and we have to decide carefully where the keyword arguments will go. Maybe they all should go into a single argument as a Dict a:000. There is also Bram's point about the caller needing to know the internal names. @brammool adding v:none was very easy; done. I thought about it for a bit, as v:none is a legitimate value someone might want to use, but it seems OK as long as it's documented. Using it for this purpose doesn't break compatibility since default values don't already exist. (Note also neovim doesn't even have v:none). |
and revert v:none
Problem: Named function arguments are never optional.
Solution: Support optional function arguments with a default value. (Andy
Massimino, closes vim/vim#3952)
vim/vim@42ae78c
vim-patch:8.1.1310: named function arguments are never optional
Problem: Named function arguments are never optional.
Solution: Support optional function arguments with a default value. (Andy
Massimino, closes vim/vim#3952)
vim/vim@42ae78c
Note: v:none is needed for supporting skipping args keeping default.
(Original vim behavior)
v:none never appears in lua and json. And if try to bring v:none to lua
or json, it will be same as null.
vim-patch:8.1.1310: named function arguments are never optional
Problem: Named function arguments are never optional.
Solution: Support optional function arguments with a default value. (Andy
Massimino, closes vim/vim#3952)
vim/vim@42ae78c
Note: v:none is needed for supporting skipping args keeping default.
(Original vim behavior)
v:none never appears in lua and json. And if try to bring v:none to lua
or json, it will be same as null.
vim-patch:8.1.1310: named function arguments are never optional
Problem: Named function arguments are never optional.
Solution: Support optional function arguments with a default value. (Andy
Massimino, closes vim/vim#3952)
vim/vim@42ae78c
Note: v:none is needed for supporting skipping args keeping default.
(Original vim behavior)
v:none never appears in lua and json. And if try to bring v:none to lua
or json, it will be same as null.
vim-patch:8.1.1310: named function arguments are never optional
Problem: Named function arguments are never optional.
Solution: Support optional function arguments with a default value. (Andy
Massimino, closes vim/vim#3952)
vim/vim@42ae78c
Note: v:none is needed for supporting skipping args keeping default.
(Original vim behavior)
v:none never appears in lua and json. And if try to bring v:none to lua
or json, it will be same as null.
Problem: Named function arguments are never optional.
Solution: Support optional function arguments with a default value. (Andy
Massimino, closes vim/vim#3952)
vim/vim@42ae78c
This is my stab at optional arguments, adding the ability to use default expressions for named arguments when using
function(mentioned in todo.txt).The expressions are evaluated each time at call time, like in ruby. So it is possible to declare a function with invalid expressions which are only detected at call, or use expressions with side-effects.
Few possible remaining issues:
- Does not work for lambdas. I'm not sure this should be allowed at all.
-
func Func(arg = g:global)works butfunc Func(arg1 = global)doesn't (might be a good thing).- Errors that occur in the default expressions do not appear to be reported as happening "inside the function." This was intentional because arguments are evaluated, then allocated in the order they appear, so one can't refer circularly. Thus,
gives
This reporting could probably be altered with some more work if it makes sense semantically. Note that "abort" does already work as expected.