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
{{ message }}
This repository was archived by the owner on Jun 5, 2023. It is now read-only.
Consider the following code (compiled with 3.1.0-RC2):
objectTest {
traitFoo[Self]
caseclassImpl1(a: String)
givenFoo[Impl1] with {}
caseclassImpl2(b: Int)
givenFoo[Impl2] with {}
objectFoo {
classTypeprivate (t: Any)
objectType {
defapply[T:Foo](t: T) =newType(t)
defunapply[T:Foo](t: Type):Option[T] =???
}
}
valx=Foo.Type(Impl1("x"))
valmatched:Option[Impl2] =Foo.Type.unapply(x) // compilesvalmatchedNoType=Foo.Type.unapply(x) // ambiguous implicit arguments (understandable)
x match {
caseFoo.Type(_: Impl2) =>???// ambiguous implicit arguments (could do better)caseFoo.Type(Impl2(_)) =>???// ambiguous implicit arguments (could do better)
}
}
In the above, Foo.Type.unapply takes an implicit type parameter that only shows up on the output. When directly invoking unapply in a context where the expected type constrains the output type, everything works. However, in a case match where the output type of unapply is effectively constrained by a nested matcher, implicit resolution still fails.
I'm not sure if there is a good reason for this. In cases where the nested unapply and/or type ascription unambiguously specifies a type constraint, I think implicit resolution could be successful.
Of course there are ambiguous cases -- for example, if we have
case Foo.Type(SomeAmbiguousUnapply(_)) => ???
where SomeAmbiguousUnapply has both unapply(Impl1) and unapply(Impl2), there is insufficient information to constrain the implicit search and it's reasonable to fail in that case.