Skip to content
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

TraversableView.isEmpty is wrong #6628

Closed
scabug opened this issue Nov 8, 2012 · 8 comments
Closed

TraversableView.isEmpty is wrong #6628

scabug opened this issue Nov 8, 2012 · 8 comments

Comments

@scabug
Copy link

scabug commented Nov 8, 2012

scala> val dropped = (new Traversable[String] { override def foreach[U](f:String
=>U) { f("1") } }).view.drop(1)
dropped: scala.collection.TraversableView[String,Traversable[String]] = Traversa
bleViewS(...)

scala> dropped.isEmpty
res0: Boolean = false

scala> dropped.force.isEmpty
res1: Boolean = true

dropped.force.isEmpty and dropped.isEmpty do not return the same result.

@scabug
Copy link
Author

scabug commented Nov 8, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6628?orig=1
Reporter: @Atry
Affected Versions: 2.10.0-RC1
Other Milestones: 2.10.0

@scabug
Copy link
Author

scabug commented Nov 10, 2012

Sam Bock (nvt) said (edited on Nov 10, 2012 5:46:55 PM UTC):
It looks like this is a side effect of the change made in this commit on TraversableViewLike, which overrides isEmpty to forward to the underlying collection. It looks like forwarding has advantages in terms of performance, and avoids forcing the first element of the view. It is not correct when a view represents some subset of the original collection, however.

The simplest solution might be to override isEmpty for the transformed views that cannot simply forward to the underlying collections, which is (I believe) all of them except for Mapped. Barring a better suggestion, these could be overridden to return to their old, foreach based behavior.

@scabug
Copy link
Author

scabug commented Nov 10, 2012

@retronym said:
Promoting to blocker, it's a regression with reasonable workaround.

@scabug
Copy link
Author

scabug commented Nov 10, 2012

@retronym said:
Not sure which way to take this one. Ideas?

@scabug
Copy link
Author

scabug commented Nov 10, 2012

@paulp said:
I see no alternative to isEmpty walking the view until it finds an element or is exhausted, unless we're willing to do something like def isEmpty = underlying match { case x: View => x.slowIsEmpty ; case _ => underlying.isEmpty } .

@scabug
Copy link
Author

scabug commented Nov 10, 2012

@retronym said:
Sounds reasonable; will you submit a reversion?

@scabug
Copy link
Author

scabug commented Nov 11, 2012

@paulp said:
Yeah.

@scabug
Copy link
Author

scabug commented Nov 15, 2012

@paulp said:
scala/scala#1636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants