-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Disallow dependent implicit methods. #1467
Conversation
The following scala script shows that scalac does not support them very well either: scala> trait T { type X; val x: X } defined trait T scala> implicit def f(x: T): x.X = x.x warning: there was one feature warning; re-run with -feature for details f: (x: T)x.X scala> val t = new T { type X = String; val x = "" } t: T{type X = String} = $anon$1@496bc455 scala> val x: String = t <console>:14: error: type mismatch; found : T{type X = String} required: String val x: String = t ^ scala> val x: String = f(t) x: String = "" Instead of doing a half-assed job, we should disallow such implicits completely. They are still supported under -language:Scala2, but type inference is less good than in scalac (i.e. we fail to find the right implicit more often).
Note that this seems to actually be used in practice: https://github.com/milessabin/shapeless/blob/master/core/src/main/scala/shapeless/poly.scala#L248 |
The change itself LGTM |
Good point about shapeless. I think we might be able to accept this On Fri, Aug 26, 2016 at 1:39 AM, Guillaume Martres <notifications@github.com
Prof. Martin Odersky |
@odersky Our discussion in SI-5070 might be relevant here. |
Allow result types of implicit methods to depend on implicit parameters.
- Add neg test against disallowed implicits - Add pos test for t5070 - Reinstantiate the depmeth oopsla tests in pending; they do not work for other reasons.
Could you explain why? |
I checked and in actually cannot find an example which we cannot do. The case where scalac fails actually compiles in Dotty, as does the example in SI-5070. The dependent method tests in pending fail for other reasons it seems. So I am closing and will integrate the new tests in another PR. |
It seems to me that you could always replace a non-implicit parameter by an implicit one with some trickery: trait T {
type X
val x: X
}
trait Box {
type Elem
}
object Box {
implicit def box[T]: Box { type Elem = T } = new Box { type Elem = T }
}
object Test {
val t = new T { type X = String; val x = "" }
def test: Unit = {
implicit def f(x: T)(implicit box: Box { type Elem = x.type }): box.Elem#X = x.x
val z: String = t // OK
}
} |
The following scala script shows that scalac does not support them very well either:
Instead of doing a half-assed job, we should disallow such implicits completely.
They are still supported under -language:Scala2, but type inference is less good
than in scalac (i.e. we fail to find the right implicit more often).