You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: news/_posts/2016-04-11-release-notes-2.12.0-M4.md
+16-20Lines changed: 16 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,34 +60,30 @@ The work on the new optimizer is still ongoing. You can track it in the [scala-
60
60
#### SAM types
61
61
As of [#4971](https://github.com/scala/scala/pull/4971), we treat Single Abstract Method types in the same way as our built-in FunctionN classes. This means overloading resolution has more contenders to choose from, making type inference less effective. Here's an example:
62
62
63
-
```scala
64
-
classC[V] {
65
-
defsort(cmp: java.util.Comparator[V]):C[V] =???
66
-
defsort(cmp: (V, V) =>Int):C[V] = sort(
67
-
new java.util.Comparator[V] {
68
-
defcompare(a: V, b: V):Int= cmp(a, b)
69
-
})
70
-
}
71
-
72
-
(newC[Int]) sort (_ - _) // error
73
-
(newC[Int]) sort ((_ - _): java.util.Comparator[Int]) // ok
74
-
(newC[Int]) sort ((a: Int, b: Int) => a - b) // ok
(new C[Int]) sort ((_ - _): java.util.Comparator[Int]) // ok
73
+
(new C[Int]) sort ((a: Int, b: Int) => a - b) // ok
76
74
77
75
The first attempt fails because the type checker cannot infer the types for `_ - _`'s arguments anymore.
78
76
Type inference in this scenario only works when we can narrow the overloads down to one before type checking the arguments the methods are applied to. When a function is passed as an argument to an overloaded method, we do this by considering the "shape" of the function (essentially, its arity). Now that `Comparator[?]` and `(?, ?) => ?` are both considered functions of arity two, our clever scheme breaks down and the programmer must either select an overload (second application) or make the argument types explicit (last application, which resolves to the `Function2` overload).
79
77
80
78
Finally, implicit conversion of SAM types to Function types won't kick in anymore, since the compiler does this conversion itself first:
81
79
82
-
```scala
83
-
traitMySam { defapply(x: Int):String }
80
+
trait MySam { def apply(x: Int): String }
84
81
85
-
implicitdefunused(fun: Int=>String):MySam
86
-
=newMySam { defapply(x: Int) = fun(x) }
82
+
implicit def unused(fun: Int => String): MySam
83
+
= new MySam { def apply(x: Int) = fun(x) }
87
84
88
-
// uses sam conversion, not the `unused` implicit
89
-
valsammy:MySam= _.toString
90
-
```
85
+
// uses sam conversion, not the `unused` implicit
86
+
val sammy: MySam = _.toString
91
87
92
88
#### Changed syntax trees (affects macro and compiler plugin authors)
-[#5251](https://github.com/scala/scala/pull/5251): concrete trait methods are compiled to a
13
14
static method (in the interface classfile) containing the actual implementation, and a default
14
15
method forwards that to the static one.
@@ -82,34 +83,30 @@ The work on the new optimizer is still ongoing. You can track it in the [scala-
82
83
#### SAM types
83
84
As of [#4971](https://github.com/scala/scala/pull/4971), we treat Single Abstract Method types in the same way as our built-in FunctionN classes. This means overloading resolution has more contenders to choose from, making type inference less effective. Here's an example:
(newC[Int]) sort ((_ - _): java.util.Comparator[Int]) // ok
96
-
(newC[Int]) sort ((a: Int, b: Int) => a - b) // ok
97
-
```
94
+
(new C[Int]) sort (_ - _) // error
95
+
(new C[Int]) sort ((_ - _): java.util.Comparator[Int]) // ok
96
+
(new C[Int]) sort ((a: Int, b: Int) => a - b) // ok
98
97
99
98
The first attempt fails because the type checker cannot infer the types for `_ - _`'s arguments anymore.
100
99
Type inference in this scenario only works when we can narrow the overloads down to one before type checking the arguments the methods are applied to. When a function is passed as an argument to an overloaded method, we do this by considering the "shape" of the function (essentially, its arity). Now that `Comparator[?]` and `(?, ?) => ?` are both considered functions of arity two, our clever scheme breaks down and the programmer must either select an overload (second application) or make the argument types explicit (last application, which resolves to the `Function2` overload).
101
100
102
101
Finally, implicit conversion of SAM types to Function types won't kick in anymore, since the compiler does this conversion itself first:
103
102
104
-
```scala
105
-
traitMySam { defapply(x: Int):String }
103
+
trait MySam { def apply(x: Int): String }
106
104
107
-
implicitdefunused(fun: Int=>String):MySam
108
-
=newMySam { defapply(x: Int) = fun(x) }
105
+
implicit def unused(fun: Int => String): MySam
106
+
= new MySam { def apply(x: Int) = fun(x) }
109
107
110
-
// uses sam conversion, not the `unused` implicit
111
-
valsammy:MySam= _.toString
112
-
```
108
+
// uses sam conversion, not the `unused` implicit
109
+
val sammy: MySam = _.toString
113
110
114
111
#### Changed syntax trees (affects macro and compiler plugin authors)
0 commit comments