Is it possible to rewrite the following example using only polymorphic functions without TypeTags? The example consists of a class A[T]
which has a method matches
returning true
when applied to an instance of A
with the same type parameter T
and false
if this type parameter has a different value. Then matches
is mapped over an hlist l
of A[T]
twice resulting in an hlist of nested hlists containing results of matching each item of l
against others:
import scala.reflect.runtime.universe._
import shapeless._
class A[T: TypeTag]{
object matches extends Poly1 {
implicit def default[U: TypeTag] = at[A[U]]{ _ => typeOf[U] <:< typeOf[T] }
}
}
val l = new A[Int] :: new A[String] :: new A[Boolean] :: HNil
object matcher extends Poly1 {
implicit def default[T] = at[A[T]]{ a => l map a.matches }
}
l map matcher
Each item has one match, i.e. the result is:
(true :: false :: false :: HNil) ::
(false :: true :: false :: HNil) ::
(false :: false :: true :: HNil) :: HNil
When I try to rewrite the example without TypeTags, matches
always uses it's no
case and return false:
import shapeless._
class A[T]{
object matches extends Poly1 {
implicit def yes = at[A[T]]{_ => true}
implicit def no[U] = at[U]{_ => false}
}
}
val l = new A[Int] :: new A[String] :: new A[Boolean] :: HNil
object matcher extends Poly1 {
implicit def default[T] = at[A[T]]{ a => l map a.matches }
}
l map matcher
The result is:
(false :: false :: false :: HNil) ::
(false :: false :: false :: HNil) ::
(false :: false :: false :: HNil) :: HNil
Is it possible to rewrite this example without TypeTags and get the same result as in the first case?