no error raised for :let list[out-of-range] = value

7 views
Skip to first unread message

Yukihiro Nakadaira

unread,
Apr 19, 2011, 5:40:30 AM4/19/11
to vim_dev
When assigning or deleting list item no error raised for out-of-range index.

:let list = []
:let list[0] = 0
:echo list
[]

:let list = []
:unlet list[0]

I expected "E684: list index out of range: 0" or similar error.

And slice assignment do nothing for empty slice.

:let list = []
:let list[0:9] = [1, 2]
:echo list
[]

:let list = [0]
:let list[9:] = [1, 2]
:echo list
[0]

:let list = []
:let list[:] = [1, 2]
:echo list
[]

For non-empty slice, value is assigned and/or error is raised instead.

:let list = [0]
:let list[0:9] = [1, 2]
E711: List value has not enough items
:echo list
[1, 2]

:let list = [0]
:let list[0:0] = [1, 2]
E710: List value has more items than target
:echo list
[1]

:let list = [0]
:let list[:] = [1, 2]
:echo list
[1, 2]


--
Yukihiro Nakadaira - yukihiro....@gmail.com

Ben Fritz

unread,
Apr 20, 2011, 8:10:32 PM4/20/11
to vim_dev


On Apr 19, 4:40 am, Yukihiro Nakadaira <yukihiro.nakada...@gmail.com>
wrote:
> When assigning or deleting list item no error raised for out-of-range index.
>
>    :let list = []
>    :let list[0] = 0
>    :echo list
>    []
>
>    :let list = []
>    :unlet list[0]
>
> I expected "E684: list index out of range: 0" or similar error.
>

This one is a feature. It lets you easily grow a list.

Ben Schmidt

unread,
Apr 20, 2011, 8:15:52 PM4/20/11
to vim...@googlegroups.com, Ben Fritz

How?

Ben.

Bram Moolenaar

unread,
Apr 21, 2011, 7:44:31 AM4/21/11
to Yukihiro Nakadaira, vim_dev

Yukihiro Nakadaira wrote:

> When assigning or deleting list item no error raised for out-of-range index.
>
> :let list = []
> :let list[0] = 0
> :echo list
> []
>
> :let list = []
> :unlet list[0]
>
> I expected "E684: list index out of range: 0" or similar error.

Indeed. Strange that nobody noticed the error message is missing.

I'm preparing a patch to fix this. Hopefully it doesn't go the other
way, giving an error message when it should not.

--
Bypasses are devices that allow some people to dash from point A to
point B very fast while other people dash from point B to point A very
fast. People living at point C, being a point directly in between, are
often given to wonder what's so great about point A that so many people
from point B are so keen to get there and what's so great about point B
that so many people from point A are so keen to get there. They often
wish that people would just once and for all work out where the hell
they wanted to be.
-- Douglas Adams, "The Hitchhiker's Guide to the Galaxy"

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Benjamin Fritz

unread,
Apr 21, 2011, 5:05:53 PM4/21/11
to vim...@googlegroups.com

Oops nevermind, I missed the part where the list remained empty even
after setting the element to a value. I did not read carefully enough
and was apparently thinking of Perl. I'd consider it a bug.

Reply all
Reply to author
Forward
0 new messages