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

Wart(s) to check for StringOps#toInt etc. #560

Open
nigredo-tori opened this issue Dec 29, 2021 · 1 comment
Open

Wart(s) to check for StringOps#toInt etc. #560

nigredo-tori opened this issue Dec 29, 2021 · 1 comment

Comments

@nigredo-tori
Copy link
Contributor

In a similar vein to OptionPartial and TryPartial, we can check for non-total conversions from String to primitive types:

// This throws
"foo".toInt
// This forces the code to handle the failure
"foo".toIntOption

Offending methods in scala.collection.StringOps are toBoolean, toByte, toDouble, toFloat, toInt, toLong and toShort.

Note that this wart would be specific to Scala 2.13, since that's when we got the to*Option syntax.

@ashleymercer
Copy link

Just thinking out loud: the wart wouldn't necessarily need to be specific to 2.13 - although it might be useful to tailor error messages to something like:

For Scala <=2.12 you should use `Try(foo.toInt)`, for Scala >= 2.13 use `foo.toIntOption`

(or possibly for forwards-compatibility, suggest Try(foo.toInt).toOption for <=2.12?)

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

No branches or pull requests

3 participants