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

IndexedSeq#head now throws NoSuchElementException (not IndexOutOfBoundsException) #10392

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/library/scala/collection/IndexedSeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,25 @@ trait IndexedSeqOps[+A, +CC[_], +C] extends Any with SeqOps[A, CC, C] { self =>

override def slice(from: Int, until: Int): C = fromSpecific(new IndexedSeqView.Slice(this, from, until))

override def head: A = apply(0)
override def head: A =

Choose a reason for hiding this comment

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

wouldn't this now lead to repeated checks for isEmpty in {head,last}Option? should there be an @inline def headImpl: A = apply(0) and used in both head and headOption?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My comment on the first commit is that head is fast. headOption is allocating.

if (!isEmpty) apply(0)
else throw new NoSuchElementException(s"head of empty ${
self match {
case self: IndexedSeq[_] => self.collectionClassName
case _ => toString
}
}")

override def headOption: Option[A] = if (isEmpty) None else Some(head)

override def last: A = apply(length - 1)
override def last: A =
if (!isEmpty) apply(length - 1)
else throw new NoSuchElementException(s"last of empty ${
self match {
case self: IndexedSeq[_] => self.collectionClassName
case _ => toString
}
}")

// We already inherit an efficient `lastOption = if (isEmpty) None else Some(last)`

Expand Down
7 changes: 7 additions & 0 deletions test/junit/scala/collection/mutable/ArrayDequeTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.junit.Assert._

import scala.annotation.nowarn
import scala.collection.SeqFactory
import scala.tools.testkit.AssertUtil.assertThrows

class ArrayDequeTest {

Expand Down Expand Up @@ -116,6 +117,12 @@ class ArrayDequeTest {
a.trimToSize() // Shrink to 256
assertEquals(a.capacity, 256)
}

@Test def `head of empty throws NoSuchElement`: Unit =
assertThrows[NoSuchElementException](ArrayDeque.empty[Int].head, _.endsWith("head of empty ArrayDeque"))

@Test def `last of empty throws NoSuchElement`: Unit =
assertThrows[NoSuchElementException](ArrayDeque.empty[Int].last, _.endsWith("last of empty ArrayDeque"))
}

object ArrayDequeTest {
Expand Down