-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add safe integral auto-coercions #225
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
- Start Date: 2014-09-03 | ||
- RFC PR #: | ||
- Rust Issue #: | ||
|
||
# Summary | ||
|
||
Enable implicit coercion from some integral type `A` to some integral type `B` if type `B` can represent all the values type `A` can. | ||
|
||
# Motivation | ||
|
||
This would improve both programming convenience and code readability. | ||
|
||
# Detailed design | ||
|
||
All unsigned integral types should auto-coerce to a wider integral type. All signed integral types should auto-coerce to a wider signed integral type. For example, `u16` would auto-coerce to any of the following: `u32`, `u64`, `i32`, `i64`. But `i16` would auto-coerce only to `i32` and `i64`. | ||
|
||
# Drawbacks | ||
|
||
? | ||
|
||
# Alternatives | ||
|
||
? | ||
|
||
# Unresolved questions | ||
|
||
What about auto-coercion to `int` and `uint`? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Drawback: silent conversions are confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the type ofbar()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I re-used the name
bar()
, that's a bit confusing. I'll fix it. I meant for the first call tobar()
to be some unknown function with an unknown return value. The expectation is if the linelet y = x + bar()
compiles, thenbar()
's return value must be compatible withx
, which is au32
, and addition on integral types is known to return the receiver type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, but let's say that
mystery_func()
returnsu64
. Today the compiler would give the error (mismatched types: expectedu32
, foundu64
) on the line:let y = x + mystery_func();
. If this RFC lands, then the compiler would give the same error on the line:bar(y);
. I don't see this (the location of the error) as a big issue; after all, you can fix your code by down-casting eithermystery_func()
ory
tou32
, so it's not that clear what the best place for the error is anyway.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is it divorces the reported error from the actual error. The line
bar(y);
is not the bad code. The linelet y = x + mystery_func();
is the bad code. Yes, you can get the same issue today merely by using a bad type, e.g. ifbar()
tookx: i32
instead ofx: u32
. But in the cited example, it looks straightforward, it looks like the typing should all match, and the error onbar(y);
is therefore confusing.