fix: allow empty range subscripts [0..-1] and [0...0] on empty lists

Add special case in list_subscript native to return a new empty list when
the source list is empty and the requested range is [0..-1] (inclusive) or
[0...0] (exclusive). This enables using list[0..-1] as a universal copy
idiom that works even for empty lists. Update the List.+ operator to use
this idiom instead of manual iteration. Add test cases for both range
forms on empty lists.
This commit is contained in:
Bob Nystrom
2014-02-15 04:10:41 +00:00
parent 5f16a3a1cc
commit 08618abc5d
3 changed files with 18 additions and 8 deletions
+1 -4
View File
@@ -10,10 +10,7 @@ class List {
}
+ that {
var result = []
for (element in this) {
result.add(element)
}
var result = this[0..-1]
for (element in that) {
result.add(element)
}