1

I am trying to capture the result returned from a function implementing range as an argument but apparently I get a comparison.

With the following code:

function! GetNumber() range
  return 4
endfunction

function! GetString() range
  return ""
endfunction

function! GetList() range
  echo a:firstline
  echo a:lastline
  return []
endfunction

After having selected some lines, I do the following operations:

  • echo '<,'>GetNumber() or let a = <,'>GetNumber(): returns 0
  • echo '<,'>GetList() or let a = <,'>GetString(): returns 1
  • echo '<,'>GetList() or let a = <,'>GetList():
    • Error E691: Can only compare List with List
    • E15: Invalid expression: '<,'>GetList().

Yet, I can access to the variables a:firstline and a:lastline in the functions. I would like to know where could be my error and also where comes this comparison?

Does the same thing on Vim 7.4 and Vim 8.

0

You're comparing the string to the result of the function: echo '<,' > GetNumber().

:echo and :let accept an expression, where the only way to call a function is funcname(args); you can't specify a range there. You could use :call, but it discards the return value. Thus the only way to get something out of such a function is to set a global variable:

function! GetList() range
  echo a:firstline
  echo a:lastline
  let g:GetListResult = []
endfunction
  • Thank you for the reply and the idea for bypassing this problem. – Nales Jul 31 '17 at 18:24

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.