feat: remove List.new(_) constructor and consolidate into List.filled(_,_)

Remove the List.new(_) primitive that initialized a list with null elements, merging its functionality into List.filled(_,_) which now accepts both size and default value. Add validation for negative size with a clear error message, and introduce comprehensive test coverage for filled list creation including edge cases for negative size, non-integer size, and non-numeric size arguments.
This commit is contained in:
Bob Nystrom
2016-08-04 05:42:31 +00:00
parent 85ab5d2aec
commit bfb109473e
6 changed files with 29 additions and 45 deletions
+8
View File
@@ -0,0 +1,8 @@
var list = List.filled(3, "value")
System.print(list.count) // expect: 3
System.print(list) // expect: [value, value, value]
// Can create an empty list.
list = List.filled(0, "value")
System.print(list.count) // expect: 0
System.print(list) // expect: []
+1
View File
@@ -0,0 +1 @@
List.filled(-1, null) // expect runtime error: Size cannot be negative.
+1
View File
@@ -0,0 +1 @@
List.filled(1.2, null) // expect runtime error: Size must be an integer.
+1
View File
@@ -0,0 +1 @@
List.filled("not num", null) // expect runtime error: Size must be a number.
-8
View File
@@ -1,8 +0,0 @@
var list = List.new(5)
System.print(list.count) // expect: 5
System.print(list) // expect: [null, null, null, null, null]
var list2 = List.filled(5, 2)
System.print(list2.count) // expect: 5
System.print(list2) // expect: [2, 2, 2, 2, 2]