Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function with list or splat arguments

Tags:

vim

Is there a way to call a vim function with a list of arguments. My list of arguments is coming from a optional splat arguments in another function, and I need a way to pass these arguments to the target function.

The target function is,

function! run_hello(cmd, ...)
  echo 'run_hello'
  echo a:cmd
  echo a:000
endfunction

The function that will call run_hello is,

function! hello(...)
  call run_hello('foo', the splats here)
endfunction

It will be called like so, with different arguments.

call hello('lorem', 'ipsum', 'dolor')

I am currently using hello(arglist) and passing the a:000 list forward. But I would like to know if it is possible to call a function with a list as arguments, that then become it's regular argument list.

Something like JavaScript's,

foo.apply(this, ['a', 'b', 'c']

Thanks.

like image 662
Darshan Sawardekar Avatar asked Aug 15 '13 09:08

Darshan Sawardekar


1 Answers

The equivalent of JavaScript's apply() is call() in Vimscript:

function! hello(...)
    call call('run_hello', ['foo'] + a:000)
endfunction

This is no typo: You :call the function named call().

like image 167
Ingo Karkat Avatar answered Sep 20 '22 05:09

Ingo Karkat