The following code does not compile on Scala 2.12 / 2.13. Why?
class X[U, T]
object X {
implicit def genericX[U, T](implicit ev: T <:< U): X[U, T] = new X[U, T]
}
implicitly[X[AnyRef, String]] // compiles
implicitly[X[String, Nothing]] // does not compile
+T
solves the problem, but unfortunately this is not what I can accept there. In fact, I would like to haveT
contravariant, but got stuck even in the invariant case. – Kamil KlochX
? Now it's basically a synonym for<:<
. Why can't you use<:<
directly? Why do you need implicit to be resolved forNothing
? How doesNothing
appear in your code? – Dmytro Mitindef check[T](....): A[T] = ???
so thatcheck(...)
returnsA[Any]
whilecheck[String](...)
returnsA[String]
. (i.e.Any
inferred as a default type, instead ofNothing
). I tried the following:def check[T](implicit ev: X[Any, T]): A[T] = ???
and henceX[U, -T].
– Kamil KlochA[T]
capturesClassTag[T]
inside. I am looking for a methoddef attribute[T](s: String): A[T]
, which returnsA[SomeDefaultType]
whenT
is missing (aor evenAny
, to make things simpler). I do have a working solution (gist.github.com/kamilkloch/a5c97d0c7cdec47f8dc4c4ac4c131674), however, it requires an intermediate class to curry type parameters. I was hoping to get rid of it with the aforementionedX[U, -T]
. – Kamil Kloch