-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reverse method on Range of Chars gives unexpected results #12473
Comments
this is the Scala 2 bug tracker, but the same behavior does occur in Scala 2 @scala/collections ? |
Just to add: scala> ('a' to 'z').toSeq.reverse
val res2: scala.collection.immutable.NumericRange[Char] = empty NumericRange z to a by �
scala> ('a' to 'z').toList.reverse
val res3: List[Char] = List(z, y, x, w, v, u, t, s, r, q, p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a) |
note that 2.12.15 doesn't have this problem |
I am torn, because on the one hand, anyway, I believe the problem is that scala> ('a' to 'z').step.toInt
val res4: Int = 1
scala> ('a' to 'z').reverse.step.toInt
val res5: Int = 65535
see above about being unsigned |
@NthPortal char is a numeric value type https://scala-lang.org/files/archive/spec/2.13/12-the-scala-standard-library.html#numeric-value-types just replying to whether it's a number. Signedness is a nice catch. |
this is actually a fundamental problem with we might be able to fix the problem for
if you want a range of codepoints, |
to clarify, in 2.12 Edit: in 2.13, |
I kind of want to override |
Just from a user's POV, I'm not aware - and I see no suggestion in the Scaladoc - that a Range of Chars being treated as a Range, as I see it, would ideally be a type of lightweight collection that will work with any type that offers an implementation - wouldn't it be nice if we could have a Range of |
Range of Double also did not succeed. I don't think this conceptual issue due to collections, but just about the I agree that range of chars is natural, in the sense that I remember Odersky saying |
(I'm repeating after @som-snytt here, but I think it bears repeating:)
references:
this leads to working-as-specified behaviors such as:
and any argument for proposed changes to the behavior |
reproduction steps
using Scala 3,
problem
Seems like all other Ranges give you a collection back of the same size when you
reverse
them, but a Range of Chars returns an empty Range. I understand you can get around this by turning it into a Seq (with memory usage implications) or usingreverseIterator
, but the above behaviour was surprising and caused a bug in my code.Similarly it would be nice if you could make a reversed range with
('z' to 'a' by -1)
or something to that effect. (Interestingly, this won't compile with a negative number, saying it needs to be a Char, but does with a positive number... 🤷)The text was updated successfully, but these errors were encountered: