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: content/polymorphic-eta-expansion.md
+22-10Lines changed: 22 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -390,27 +390,39 @@ This section should also argue to what extent backward source compatibility is p
390
390
391
391
#### Binary and TASTy
392
392
393
-
As this proposal never generates code that couldn't have been written before, these changes are binary and TASTy compatible.
393
+
As this proposal never generates code that couldn't have been written by hand before, these changes are binary and TASTy compatible.
394
394
395
395
#### Source
396
396
397
-
This proposal conserves source compatibility when an expected type is present, since either the behaviour is the same, or the code did not compile before.
397
+
This proposal conserves source compatibility when a non-polymorphic type is present, since by definition the behaviour is the same.
398
398
399
-
This proposal breaks source compatibility only when there is no expected type, in this regard it is essentially a change in type inference, and therefore has all the usual repercussions of changing type inference, for example:
399
+
In the case the expected type is polymorphic, either the code did not compile before, or there was an implicit conversion from the inferred monomorphic function to the expected polymorphic function. In the latter case, source compatibility is broken, since polymorphic eta-expansion will apply before search for implicit conversions, for example:
400
400
401
401
```scala
402
-
deffoo[T](x: T):T= x
403
-
valvoo= foo // was Any => Any is now [T] => T => T
// before: method is eta-expanded to Any => Any, and then converted using conv to [T] => T => T
410
+
// now: method is eta-expanded to [T] => T => T (conv is not called)
411
+
```
412
+
413
+
When there is no expected type, this proposal breaks source compatibility, in this regard it is essentially a change in type inference, and therefore has all the usual repercussions of changing type inference, for example:
414
+
415
+
```scala
416
+
defmethod[T](x: T):T= x
417
+
valfunction= method // was Any => Any is now [T] => T => T
418
+
valy= function(5) // was Any is now Int
405
419
406
420
deflookFor[T](x: T)(usingu: T):T= u
407
-
lookFor(foo) // was searching for Any => Any, is now searching for [T] => T => T
421
+
lookFor(method) // was searching for Any => Any, is now searching for [T] => T => T
408
422
// This change in search can find different instances, and thus potentially wildly different behaviour !
409
423
```
410
424
411
-
While these examples might seem damming, this is the case every time we change type inference.
412
-
413
-
**Note:** All of this disappears if we change `val voo` to `val voo: Any => Any`, this is the reason it is recommended for library authors to always provide explicit types for all public members.
425
+
While these examples might seem damming, this is no different than any other change to type inference.
0 commit comments