Skip to content

Instantly share code, notes, and snippets.

@DmytroMitin
Forked from kamilkloch/Bottom.scala
Created June 5, 2020 22:58
Show Gist options
  • Save DmytroMitin/9b290ac12ba4af3a4eb63958a04c78f9 to your computer and use it in GitHub Desktop.
Save DmytroMitin/9b290ac12ba4af3a4eb63958a04c78f9 to your computer and use it in GitHub Desktop.
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