开发者

Why does extend() engage in bizarre behaviour when passed the same list twice?

开发者 https://www.devze.com 2022-12-25 07:55 出处:网络
I\'m pretty confused by one of the subtleties of the vimscript extend() function. If you use it to extend a list with another list, it does pretty much what you\'d expect, which is to insert the seco

I'm pretty confused by one of the subtleties of the vimscript extend() function.

If you use it to extend a list with another list, it does pretty much what you'd expect, which is to insert the second list into the first list at the index given by the third parameter:

      let list1 = [1,2,3,4,5,6] | echo extend(list1,[1,2,3,4,5,6],5)
      " [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 6]

However if you give it the same list twice it starts tripping out a bit.

      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,0)
      " [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,1)
      " [1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,2)
      " [1, 2, 1, 2, 1, 2, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,3)
      " [1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,4)
      " [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 5, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,5)
      " [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 6]
      let list1 = [1,2,3,4,5,6] | echo extend(list1,list1,6)
      " [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]

Extra-confusingly, this behaviour applies when the list is referenced with two different variables:

      let list1 = [1,2,3,4,5,6] | let list2 = list1 | echo extend(list1,list2,4)
      " [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 5, 6]

This is totally bizarre to me. I can't fathom a use for this functionality, and it seems like it would be really easy to invoke it by accident when you just wanted to insert one list into another and didn't realize that the variables were referencing the same array.

The documentation says the following:

If they are |Lists|: Append {expr2} to {expr1}.

If {expr3} is given insert the items of {expr2} before item {expr3} in {expr1}.

When {expr3} is zero insert before the first item.

When {expr3} is equal to len({expr1}) then {expr2} is appended.

Examples:

        :echo sort(extend(mylist, [7, 5]))
        :call extend(mylist, [2, 3], 1)

When {expr1} is the same List as {expr2} then the number of items copied is equal to the original length of the List.

E.g., when {expr3} is 1 you get N new copies of the first item (where N is th开发者_开发百科e original length of the List).


Does this make sense in a way that I'm not getting, or is it just an eccentricity?


I think I actually figured this out as I was posting, but since I had the question all edited and all, I figured I might as well go through with it.

extend() modifies the first list in place. The second parameter is read-only. So it looks like what it's doing is duplicating the elements of the second list one by one as it inserts them into the first one. When both of those are the same lists, this doesn't work so well. But it's not smart enough to deal with this edge case, so things get whack.

I haven't found any documentation to back this up, but the hypothesis matches the evidence. I guess I could go read the source code for more info.

This is still kind of weird since I would have thought that since these are "lists" it would work a bit differently, eg duplicating the entire list first and then plunking it down into the first one by just swapping around the pointers for the head/tail. This probably wouldn't be any faster but it would take care of the edge case. Maybe it's a feature, though. I'm still open to suggestions of reasons that this could actually be useful.

Otherwise I guess I'll file a bug report. Anybody know if vim has a public bug tracker somewhere? I feel weird writing to the email address without checking to see if it's a known issue first.

0

精彩评论

暂无评论...
验证码 换一张
取 消