Bug of vim script

9 views
Skip to first unread message

Shidong Wang

unread,
Sep 15, 2019, 2:55:23 AM9/15/19
to vim_dev
Hello, I am not sure if it is a bug, but it always make me confuses:

here is a exmaple:

func Test()
  let c = 'sss'
  let a = 1
  let b = 2
  echo c[a:b]
endf

then call Test(), you will get error, Undefined variable: a:b

of cause this can be avoided by changing the last line to echo c[a : b]



--
Best regards,
wsdjeg

Tony Mechelynck

unread,
Sep 15, 2019, 3:48:44 AM9/15/19
to vim_dev, Bram Moolenaar
On Sun, Sep 15, 2019 at 8:55 AM Shidong Wang <wsd...@outlook.com> wrote:
>
> Hello, I am not sure if it is a bug, but it always make me confuses:
>
> here is a exmaple:
>
> func Test()
> let c = 'sss'
> let a = 1
> let b = 2
> echo c[a:b]
> endf
>
> then call Test(), you will get error, Undefined variable: a:b
>
> of cause this can be avoided by changing the last line to echo c[a : b]

Hm, the help defines an expr8 of that type as expr8[expr1a : expr1b]
(with spaces around the colon) then proceeds to give examples, all of
which have only empty or numeric indices without spaces around the
colon.

Looks like spaces are better (less confusing for the Vim parser,
maybe) whenever the index is _other_ than empty or purely numeric.

Bram: Bug or feature?


Best regards,
Tony.

John Little

unread,
Sep 15, 2019, 4:15:30 AM9/15/19
to vim_dev
a: is a scope, like g: or s:, for arguments of the function. For example:

function WithArgs(first, second)
echo a:first a:second
echo a:1 a:2
endfunction

Regards, John Little

Tony Mechelynck

unread,
Sep 15, 2019, 4:16:28 AM9/15/19
to vim_dev, Bram Moolenaar
Gotcha! In a function, a:b means "the argument named b". However,
c[a:b] is an error even outside a function.

c[a:b] is an error because a: is a known namespace (function arguments)
c[b,d] is an error because b is a known namespace (buffer variables)
c[x:y] is not an error, even with no spaces.

Best regards,
Tony.
Reply all
Reply to author
Forward
0 new messages