mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-12 06:38:45 +01:00
Allow "*" on lists and strings to repeat them.
This is not implemented on Sequence because, at least for lists and strings, I think users expect an eager result. Multiplying a string should give you back a string, not a lazy sequence of repeated characters. This also mirrors "+" on strings and lists, which is eager. I like the idea of having a general guideline that operators are eager. Repetition is useful for arbitrary sequences, but for that maybe we should add a "repeat()" method.
This commit is contained in:
3
test/core/string/multiply.wren
Normal file
3
test/core/string/multiply.wren
Normal file
@ -0,0 +1,3 @@
|
||||
System.print("|" + "abc" * 0 + "|") // expect: ||
|
||||
System.print("abc" * 1) // expect: abc
|
||||
System.print("abc" * 4) // expect: abcabcabcabc
|
||||
1
test/core/string/multiply_negative.wren
Normal file
1
test/core/string/multiply_negative.wren
Normal file
@ -0,0 +1 @@
|
||||
"abc" * -3 // expect runtime error: Count must be a non-negative integer.
|
||||
1
test/core/string/multiply_not_int.wren
Normal file
1
test/core/string/multiply_not_int.wren
Normal file
@ -0,0 +1 @@
|
||||
"abc" * 1.2 // expect runtime error: Count must be a non-negative integer.
|
||||
1
test/core/string/multiply_not_num.wren
Normal file
1
test/core/string/multiply_not_num.wren
Normal file
@ -0,0 +1 @@
|
||||
"abc" * "not num" // expect runtime error: Count must be a non-negative integer.
|
||||
Reference in New Issue
Block a user