Created
June 5, 2020 21:56
-
-
Save kamilkloch/d4950417ee420fc3fc2a31089b918ef4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.reflect.ClassTag | |
object Bottom extends App { | |
class C[T](implicit ev: reflect.ClassTag[T]) { | |
def ct: reflect.ClassTag[T] = ev | |
} | |
class X[U, -T] | |
object X { | |
implicit def genericX[U, T <: U](implicit ev: T <:< U): X[U, T] = new X[U, T] | |
} | |
type Bottom <: Nothing | |
implicit val bottomClassTag: ClassTag[Bottom] = implicitly[ClassTag[Nothing]] | |
implicitly[X[AnyRef, AnyRef] <:< X[AnyRef, Bottom]] // compiles | |
implicitly[X[String, Bottom]] // compiles | |
def check[T >: Bottom <: AnyRef](implicit ev: X[AnyRef, T], ct: ClassTag[T]): C[T] = new C | |
println(check.ct) // compiler infers T == Nothing, (and not T == AnyRef), despite contravariant T | |
println(check[String].ct) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment