-
Notifications
You must be signed in to change notification settings - Fork 21
MapView.values is not lazy and forcing evaluation #12059
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
Comments
We could argue that |
It is currently returning an It is confusing that when you expect operations under |
|
it actually is lazy (the implementation just returns an scala> Map(1 -> 1).view.mapValues(i => { println(i); i }).values eq ""
^
warning: Iterable[Int] and String are unrelated: they will most likely never compare equal
val res0: Boolean = false
scala> Map(1 -> 1).view.mapValues(i => { println(i); i }).values
1
val res1: Iterable[Int] = Iterable(1) |
technically this is the result of |
this problem is actually slightly more complicated than just blaming it on |
@NthPortal You are right in the fact that values is lazy and return just an iterator. My original example is invalid. But the problem arises after you call the values. Any operation after values will be forced. E.g.
This will force and return a List. On the contrary,
returns a lazy View. Here is a full test (put in a scala file and run to avoid REPL evaluation):
This will return:
As you can see values does not force, but any subsequent operation will do |
I definitely agree that this behavior makes it hard to reason about when things get evaluated. I hope not much code relies on the existing behavior and that we can change it to return a Would someone be interested in submitting a PR with the change (returning a proper view without changing the type signature of the operation)? Then we can see if something breaks in the community build or not. |
yeah, I can do that |
@NthPortal Thanks for the PR! @julienrf It is pretty easy to migrate if there is existing code relying on the current behavior. Just replace |
reproduction steps
using Scala 2.13.2,
problem
Expected behavior:
View operations should be more lazy.
values
method should return aView
(possibly a new typeMapView.Values
) without forcing the elements. E.g.Currently I use the workaround as:
The text was updated successfully, but these errors were encountered: