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

int::abs should return an unsigned int #2353

Closed
catamorphism opened this issue May 4, 2012 · 2 comments
Closed

int::abs should return an unsigned int #2353

catamorphism opened this issue May 4, 2012 · 2 comments
Labels
A-type-system Area: Type system

Comments

@catamorphism
Copy link
Contributor

Currently, int::abs has a return type that's the same as its arg type, and writing int::abs(x) as uint looks very silly. I understand that it's not totally straightforward to write what we really want (basically, we want a type function that relates an int type of a given size to a uint type of a given size, and AFAICT we can't do that), but it would still be good. Maybe there's a clever workaround I'm not seeing...

@nikomatsakis
Copy link
Contributor

I am not sure if this makes sense. I imagine a common use of abs is in some formulas where the result will still be combined with other ints, not uints.

I imagine it is actually very unusual to have code where you have an int which could be negative and you need to supply that int to a uint context such that if the int is negative you want the absolute value. I could imagine something like int::to_uint() which asserted that the int was indeed positive... currently we just do i as uint which is of course unsafe in this regard.

@catamorphism
Copy link
Contributor Author

I'll take your word for it. Closing.

celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
…ng#2353)

Co-authored-by: Adrian Palacios <73246657+adpaco-aws@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants