-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New specification for dropped weak conformance. #5358
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
odersky
merged 1 commit into
scala:master
from
dotty-staging:new-spec-for-weak-conformance
Nov 1, 2018
Merged
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,46 @@ | ||
--- | ||
layout: doc-page | ||
title: Dropped: Weak Conformance - More Details | ||
--- | ||
|
||
To simplify the underlying type theory, Dotty drops the notion of weak | ||
conformance altogether. Instead, it provides more flexibility when | ||
assigning a type to a constant expression. The new rule is: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good if this rule declared its name, which I think is "harmonize" (and also known as "new weak conformance"). |
||
|
||
- If a list of expressions `Es` appears as one of | ||
|
||
- the elements of a vararg parameter, or | ||
- the alternatives of an if-then-else or match expression, or | ||
- the body and catch results of a try expression, | ||
|
||
|
||
and all expressions have primitive numeric types, but they do not | ||
all have the same type, then the following is attempted: | ||
|
||
- the expressions `Es` are partitioned into `Int` literals on the | ||
one hand, and all other expressions on the other hand | ||
- if all the other expressions have the same numeric type `T` | ||
(which can be one of `Byte`, `Short`, `Int`, `Long`, `Float`, | ||
`Double`), possibly after widening, and if none of the `Int` | ||
literals would incur a loss of precision when converted to `T`, | ||
then they are thus converted (the other expressions are left | ||
unchanged regardless) | ||
- otherwise, the expressions `Es` are used unchanged | ||
|
||
A loss of precision occurs for an `Int -> Float` conversion of a constant | ||
`c` if `c.toFloat.toInt != c`. For an `Int -> Byte` conversion it occurs | ||
if `c.toByte.toInt != c`. For an `Int -> Short` conversion, it occurs | ||
if `c.toShort.toInt != c`. | ||
|
||
__Examples:__ | ||
|
||
inline val b = 33 | ||
def f(): Int = b + 1 | ||
List(b, 33, 5.5) : List[Double] // b is an inline val | ||
List(f(), 33, 5.5) : List[AnyVal] // f() is not a constant | ||
List(5, 11L) : List[Long] | ||
List(5, 11L, 5.5) : List[AnyVal] // Long and Double found | ||
List(1.0f, 2) : List[Float] | ||
List(1.0f, 1234567890): List[AnyVal] // loss of precision | ||
List(b, 33, 'a') : List[AnyVal] // Char is not a numeric | ||
List(5.toByte, 11) : List[Byte] |
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
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.
I just realized that in the spec, the definition of numeric widening depends on the definition of weak conformance, from https://www.scala-lang.org/files/archive/spec/2.12/06-expressions.html:
So we'll need to rewrite that definition (relatedly, I'm also trying to restrict what numeric widening can do in scala/scala#7405)
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.
Actually it seems that we simply don't have any notion of numeric widening in Dotty, we just rely on the implicit conversions defined in Int.scala/Float.scala/etc