Skip to content

Allow default expressions for named arguments#3952

Closed
andymass wants to merge 5 commits intovim:masterfrom
andymass:default-args
Closed

Allow default expressions for named arguments#3952
andymass wants to merge 5 commits intovim:masterfrom
andymass:default-args

Conversation

@andymass
Copy link

This is my stab at optional arguments, adding the ability to use default expressions for named arguments when using function (mentioned in todo.txt).

    e.g., func Func(mandatory, optional = 10 + 20, other = a:optional)

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 but func 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,

function! Func(arg1 = error, arg2 = 10)
    echo arg2
endfunction

gives

E121: Undefined variable: error
Error detected while processing function Func:
line    1:
E121: Undefined variable: arg2

This reporting could probably be altered with some more work if it makes sense semantically. Note that "abort" does already work as expected.

        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-io
Copy link

codecov-io commented Feb 14, 2019

Codecov Report

Merging #3952 into master will increase coverage by 0.21%.
The diff coverage is 84.21%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
src/userfunc.c 87.85% <84.21%> (ø) ⬆️
src/memfile.c 78.84% <0%> (-4.06%) ⬇️
src/misc1.c 78.91% <0%> (-0.84%) ⬇️
src/search.c 80.28% <0%> (-0.58%) ⬇️
src/misc2.c 85.42% <0%> (-0.37%) ⬇️
src/main.c 65.08% <0%> (-0.33%) ⬇️
src/ex_getln.c 76.01% <0%> (-0.1%) ⬇️
src/gui_gtk_x11.c 48.27% <0%> (-0.1%) ⬇️
src/regexp_nfa.c 91.61% <0%> (-0.09%) ⬇️
src/undo.c 83.22% <0%> (-0.07%) ⬇️
... and 31 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 943e963...a8b4dc6. Read the comment docs.

@mattn
Copy link
Member

mattn commented Feb 14, 2019

Interesting. Do you have plan to suppot keyword arguments?

call Log(10, base=10)

@k-takata
Copy link
Member

@andymass
Copy link
Author

@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).

@brammool brammool closed this in 42ae78c May 9, 2019
LumaKernel pushed a commit to LumaKernel/neovim that referenced this pull request Jan 28, 2020
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
LumaKernel pushed a commit to LumaKernel/neovim that referenced this pull request Jan 28, 2020
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.
LumaKernel pushed a commit to LumaKernel/neovim that referenced this pull request Jan 28, 2020
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.
LumaKernel pushed a commit to LumaKernel/neovim that referenced this pull request Jan 28, 2020
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.
LumaKernel pushed a commit to LumaKernel/neovim that referenced this pull request Jan 29, 2020
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.
janlazo pushed a commit to janlazo/neovim that referenced this pull request Feb 22, 2021
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants