Thanks to visit codestin.com
Credit goes to github.com

Skip to content

On the empty string, .tail and .init now throw (instead of returning the empty string) #10851

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

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

sh0hei
Copy link
Contributor

@sh0hei sh0hei commented Aug 31, 2024

@scala-jenkins scala-jenkins added this to the 2.13.16 milestone Aug 31, 2024
* @throws NoSuchElementException if the string is empty.
* @note $unicodeunaware
*/
def tail: String = if(s.isEmpty) throw new NoSuchElementException("tail of empty String") else slice(1, s.length)
Copy link
Contributor

Choose a reason for hiding this comment

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

Most collections follow IterableOps in throwing UnsupportedOperation (but NSE for head and last).

A couple deviate and throw NSE. It's fine to leave it as it is here.

Example showing difficulty of enforcing uniformity of behavior:

scala> 0.until(0).tail
java.util.NoSuchElementException: tail on empty Range
  at scala.collection.immutable.Range$.scala$collection$immutable$Range$$emptyRangeError(Range.scala:631)
  at scala.collection.immutable.Range.tail(Range.scala:144)
  ... 30 elided

scala> 0.to(0).tail
val res4: scala.collection.immutable.Range = empty Range 0 until 0

example with descriptive message:

scala> collection.immutable.ArraySeq(Array(42)).tail.tail
java.lang.UnsupportedOperationException: tail of empty array
  at scala.collection.ArrayOps$.tail$extension(ArrayOps.scala:346)
  at scala.collection.immutable.ArraySeq.tail(ArraySeq.scala:242)
  ... 30 elided

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant to add, I'll merge as-is if you don't change it. Merges are paused until 2.13.15 is released, I assume.

* @throws NoSuchElementException if the string is empty.
* @note $unicodeunaware
*/
def tail: String = if(s.isEmpty) throw new NoSuchElementException("tail of empty String") else slice(1, s.length)
Copy link
Contributor

Choose a reason for hiding this comment

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

I meant to add, I'll merge as-is if you don't change it. Merges are paused until 2.13.15 is released, I assume.

Copy link
Member

@Philippus Philippus left a comment

Choose a reason for hiding this comment

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

most "of empty" exceptions lowercase the type, e.g. "tail of empty array" / "tail of empty list". Would be nice to keep that consistent and use "tail of empty string" / "init of empty string".

@som-snytt
Copy link
Contributor

Nice observation. Exception as shown is empty Range. If I manage to follow up with a uniform behavior test, I'll include that condition.

I always have a hiccup on String vs string, and favor String FSR. I would probably prefer naming a type, empty Map, but there is an argument to be made for either choice. I am not above: "I need a kleenex."

@sh0hei sh0hei requested a review from som-snytt September 1, 2024 15:33
@sh0hei sh0hei changed the title Throw exception on init last when empty string Throw exception on tail init when empty string Sep 2, 2024
@SethTisue SethTisue requested a review from a team September 2, 2024 09:53
@SethTisue SethTisue self-assigned this Nov 6, 2024
@SethTisue SethTisue force-pushed the throw-exception-empty-string branch from 26442d9 to 0e088bc Compare November 6, 2024 18:15
@SethTisue
Copy link
Member

SethTisue commented Nov 6, 2024

It is rather bewildering trying to figure out what the "right" or "best" exception to throw is here, but I'll go along with the UnsupportedOperationException suggestion.

I have taken the liberty of squashing and force-pushing, so we can hit merge as soon as CI likes it.

I will release-note this since it wouldn't surprise me if there is code out there inadvertently relying on the old behavior.

@SethTisue SethTisue added release-notes worth highlighting in next release notes library:base Changes to the base library, i.e. Function Tuples Try labels Nov 6, 2024
@som-snytt som-snytt merged commit b4b54a6 into scala:2.13.x Nov 25, 2024
3 checks passed
Copy link
Contributor

@som-snytt som-snytt left a comment

Choose a reason for hiding this comment

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

I merged on Seth's approval, but normally I would have complained that there is no space after if.

@SethTisue SethTisue changed the title Throw exception on tail init when empty string On the empty string, .tail and .init now throw (instead of returning the empty string) Dec 19, 2024
@sh0hei sh0hei deleted the throw-exception-empty-string branch January 21, 2025 07:46
hamzaremmal pushed a commit to hamzaremmal/scala3 that referenced this pull request May 2, 2025
…y-string

Throw exception on tail init when empty string
hamzaremmal pushed a commit to scala/scala3 that referenced this pull request May 7, 2025
…y-string

Throw exception on tail init when empty string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library:base Changes to the base library, i.e. Function Tuples Try release-notes worth highlighting in next release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Empty string tail and init don't throw (unlike WrappedString)
5 participants