4

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.

4

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
    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 Sep 12 '15 at 12:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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