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

Ints.checkedCast vs. Math.toIntExact #3105

Open
Stephan202 opened this issue Apr 7, 2018 · 3 comments
Open

Ints.checkedCast vs. Math.toIntExact #3105

Stephan202 opened this issue Apr 7, 2018 · 3 comments

Comments

@Stephan202
Copy link
Contributor

As of Java 8 there is Math.toIntExact. This method forms an alternative to Guava's Ints.checkedCast, except that the former throws an ArithmeticException while the latter throws an IllegalArgumentException.

Suggestion: update the documentation of Ints.checkedCast to suggest Math.toIntExact as an alternative for Java 8 users, pointing out the caveat that this would change the exception behavior.

@lowasser
Copy link
Contributor

lowasser commented May 18, 2018

Hmmm. This feels a little weird, largely because most of our classes in primitives have a checkedCast method, and it's only Ints that has an equivalent in Java 8. Eh, I think it's still worth it.

@kluever
Copy link
Member

kluever commented May 20, 2018

Should we consider adding this warning to the other (now obsolete) APIs in places like IntMath though? e.g., IntMath.checkedSubtract(a, b) -> Math.subtractExact(a, b)

@kluever
Copy link
Member

kluever commented May 20, 2018

Oh, looks like you already have a CL out to do so!

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

5 participants