5

I am trying to achieve the following :

  1. Having a set of predefined functions in my script
  2. Having a map of relation between the functions and their argument
  3. Calling the functions with their argument dynamically

Here is the code that I have :

function! s:A(a) abort
  return 'A'.a:a
endfunction

function! s:B(b) abort
  return 'B'.a:b
endfunction

let s:functions_map ={
      \ 'A': 'aa',
      \ 'B': 'bb'
      \ }

function! s:CallMap() abort
  for [l:key, l:value] in items(s:functions_map)
    let l:Current_function = function(l:key)
    echo l:Current_function(l:value)
  endfor
endfunction

command! Call call <SID>CallMap()

When I run Call I get the following message :

E700: Unknown function: A

Do you know where the issue could be coming from ?

Edit : I replace l:current_function by l:Current_function to solve another error that was not directly related to the problem.

2 Answers 2

5

You defined the function s:A, but tried to call function('A') - they are not the same: function A() doesn't exist. (:h E700 points directly to the function() call)

You'll probably have to use '<SID>A' in place of 'A' as key (same for B, of course).

Also, :h call() (with braces; not call) might help with the handling of the function arguments.

1
  • 2
    Right ! I replace my function creation with function('<SID>'.l:key) and it worked :) +1 for the call() it will replace my function creation.
    – nobe4
    Commented Sep 12, 2015 at 12:07
0

Adding another simple example to elaborate the previous solutions.

" -------------------------------------
" These are representative functions
function! Test0()
  echom "This is Test0"
endfunction

function! Test1()
  echom "This is Test1"
endfunction

function! Test2()
  echom "This is Test2"
endfunction
" -------------------------------------
function! Test(func_name_str)
  echom "This is Test"
  " Note: function reference name should start with Captical letter
  " Create function reference
  let FuncRef = function(a:func_name_str)
  " Call the function using its reference
  call FuncRef()
endfunction
" -------------------------------------

Source above code. And use it as below,

:call Test('Test0')
:call Test('Test1')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.