@@ -40,7 +40,8 @@ assigning a type to a constant expression. The rule is:
40
40
and all expressions have primitive numeric types, but they do not
41
41
all have the same type, then the following is attempted: Every
42
42
constant expression ` E ` in ` Es ` is widened to the least primitive
43
- numeric value type above the types of all expressions in ` Es ` . Here
43
+ numeric value type equal to or above the types of all expressions in ` Es ` ,
44
+ if that can be done without a loss of precision. Here
44
45
_ above_ and _ least_ are interpreted according to the ordering given
45
46
below.
46
47
@@ -55,24 +56,32 @@ assigning a type to a constant expression. The rule is:
55
56
|
56
57
Byte
57
58
58
- If these widenings lead to all expressions ` Es ` having the same type,
59
- we use the transformed list of expressions instead of ` Es ` , otherwise
60
- we use ` Es ` unchanged.
59
+ A loss of precision occurs for an ` Int -> Float ` conversion of a constant
60
+ ` c ` if ` c.toFloat.toInt != c ` . For a ` Long -> Double ` conversion it occurs
61
+ if ` c.toDouble.toLong != c ` .
62
+
63
+ If these widenings lead to all widened expressions having the same type,
64
+ we use the widened expressions instead of ` Es ` , otherwise we use ` Es ` unchanged.
61
65
62
66
__ Examples:__
63
67
64
- inline val b: Byte = 3
65
- inline val s: Short = 33
66
- def f(): Int = b + s
67
- List(b, s, 'a') : List[Int]
68
- List(b, s, 'a', f()): List[Int]
69
- List(1.0f, 'a', 0) : List[Float]
70
- List(1.0f, 1L) : List[Double]
71
- List(1.0f, 1L, f()) : List[AnyVal]
72
-
73
- The expression on the last line has type ` List[AnyVal] ` , since widenings
74
- only affect constants. Hence, ` 1.0f ` and ` 1L ` are widened to ` Double ` ,
75
- but ` f() ` still has type ` Int ` . The elements don't agree on a type after
76
- widening, hence the expressions are left unchanged.
68
+ inline val b = 33
69
+ def f(): Int = b + 1
70
+ List(b, 33, 'a') : List[Int]
71
+ List(b, 33, 'a', f()) : List[Int]
72
+ List(1.0f, 'a', 0) : List[Float]
73
+ List(1.0f, 1L) : List[Double]
74
+ List(1.0f, 1L, f()) : List[AnyVal]
75
+ List(1.0f, 1234567890): List[AnyVal]
76
+
77
+ The expression on the second-to-last line has type ` List[AnyVal] ` ,
78
+ since widenings only affect constants. Hence, ` 1.0f ` and ` 1L ` are
79
+ widened to ` Double ` , but ` f() ` still has type ` Int ` . The elements
80
+ don't agree on a type after widening, hence the elements are left
81
+ unchanged.
82
+
83
+ The expression on the last line has type ` List[AnyVal] ` because
84
+ ` 1234567890 ` cannot be converted to a ` Float ` without a loss of
85
+ precision.
77
86
78
87
0 commit comments