@@ -27,62 +27,16 @@ Here, it is less clear why the type should be widened to
27
27
` List[Double] ` , a ` List[AnyVal] ` seems to be an equally valid -- and
28
28
more principled -- choice.
29
29
30
- To simplify the underlying type theory, Dotty drops the notion of weak
31
- conformance altogether. Instead, it provides more flexibility when
32
- assigning a type to a constant expression. The new rule is:
33
-
34
- - If a list of expressions ` Es ` appears as one of
35
-
36
- - the elements of a vararg parameter, or
37
- - the alternatives of an if-then-else or match expression, or
38
- - the body and catch results of a try expression,
39
-
40
-
41
- and all expressions have primitive numeric types, but they do not
42
- all have the same type, then the following is attempted: Every
43
- constant expression ` E ` in ` Es ` is widened to the least primitive
44
- numeric value type equal to or above the types of all expressions in ` Es ` ,
45
- if that can be done without a loss of precision. Here
46
- _ above_ and _ least_ are interpreted according to the ordering given
47
- below.
48
-
49
-
50
- Double
51
- / \
52
- Long Float
53
- \ /
54
- Int
55
- / \
56
- Short Char
57
- |
58
- Byte
59
-
60
- A loss of precision occurs for an ` Int -> Float ` conversion of a constant
61
- ` c ` if ` c.toFloat.toInt != c ` . For a ` Long -> Double ` conversion it occurs
62
- if ` c.toDouble.toLong != c ` .
63
-
64
- If these widenings lead to all widened expressions having the same type,
65
- we use the widened expressions instead of ` Es ` , otherwise we use ` Es ` unchanged.
66
-
67
- __ Examples:__
68
-
69
- inline val b = 33
70
- def f(): Int = b + 1
71
- List(b, 33, 'a') : List[Int]
72
- List(b, 33, 'a', f()) : List[Int]
73
- List(1.0f, 'a', 0) : List[Float]
74
- List(1.0f, 1L) : List[Double]
75
- List(1.0f, 1L, f()) : List[AnyVal]
76
- List(1.0f, 1234567890): List[AnyVal]
77
-
78
- The expression on the second-to-last line has type ` List[AnyVal] ` ,
79
- since widenings only affect constants. Hence, ` 1.0f ` and ` 1L ` are
80
- widened to ` Double ` , but ` f() ` still has type ` Int ` . The elements
81
- don't agree on a type after widening, hence the elements are left
82
- unchanged.
83
-
84
- The expression on the last line has type ` List[AnyVal] ` because
85
- ` 1234567890 ` cannot be converted to a ` Float ` without a loss of
86
- precision.
87
-
88
-
30
+ Weak conformance applies to all "numeric" types (including ` Char ` ), and
31
+ independently of whether the expressions are literals or not. However,
32
+ in hindsight, the only intended use case is for * integer literals* to
33
+ be adapted to the type of the other expressions. Other types of numerics
34
+ have an explicit type annotation embedded in their syntax (` f ` , ` d ` ,
35
+ ` . ` , ` L ` or ` ' ` for ` Char ` s) which ensures that their author really
36
+ meant them to have that specific type).
37
+
38
+ Therefore, Dotty drops the general notion of weak conformance, and
39
+ instead keeps one rule: ` Int ` literals are adapted to other numeric
40
+ types if necessary.
41
+
42
+ [ More details] ( weak-conformance-spec.html )
0 commit comments