Skip to content

Add TraversableOnce.knownSize #143

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

Closed
wants to merge 3 commits into from

Conversation

MasseGuillaume
Copy link
Contributor

No description provided.

Copy link
Contributor

@julienrf julienrf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, can you add a simple test?

@MasseGuillaume
Copy link
Contributor Author

Let's put this PR on hold: scala/bug#11065. It looks like knownSize is not correctly implemented in 2.13.

class ExtensionsTest {
@Test
def `TraversableOnce.knownSize`(): Unit = {
Assert.assertEquals(-1, Iterator(1).knownSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 2.13 the size of an IndexedSeq’s iterator is known: https://github.com/scala/scala/blob/2.13.x/src/library/scala/collection/IndexedSeqView.scala#L27

You can use Iterator.from(1) instead.

@Test
def `TraversableOnce.knownSize`(): Unit = {
Assert.assertEquals(-1, Iterator(1).knownSize)
Assert.assertEquals(1, List(1).knownSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size of LinearSeqs is not known in 2.13.

Actually I realize only now that hasDefiniteSize has nothing to do with the fact that the size of the collection is cheaply computable. It only tells us whether the collection is finite or infinite. Consequently, this is not the correct way to implement knownSize. I’m not sure there is a correct way.

martijnhoekstra pushed a commit to martijnhoekstra/scala-collection-compat that referenced this pull request Nov 9, 2022
it isn't obvious to me from the git history how/why this even
was there in the first place
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants