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:
Bob Nystrom
2016-02-24 07:48:03 -08:00
parent 919605b7ba
commit d46dfc9500
12 changed files with 74 additions and 15 deletions

View File

@ -0,0 +1,3 @@
System.print("|" + "abc" * 0 + "|") // expect: ||
System.print("abc" * 1) // expect: abc
System.print("abc" * 4) // expect: abcabcabcabc

View File

@ -0,0 +1 @@
"abc" * -3 // expect runtime error: Count must be a non-negative integer.

View File

@ -0,0 +1 @@
"abc" * 1.2 // expect runtime error: Count must be a non-negative integer.

View File

@ -0,0 +1 @@
"abc" * "not num" // expect runtime error: Count must be a non-negative integer.