jeudi 16 juillet 2015

How to compare potentially erased type against type bound?

This compiles and runs fine:

def allNamesOfClass[A : ClassTag]: Seq[String] =
  if (classTag[A] <:< classTag[HasName])
    . . .

except that the compiler complains that <:< has been deprecated for ClassTags. How can I do this right?

If I change ClassTag to TypeTag (and classTag to typeTag), the compiler complains about calling code that doesn't have access to the erased type. Is there a way to convert a ClassTag to a TypeTag? (If not, why not? I've been having great difficulty finding such a conversion, so I figure there's probably a reason for that.) Is there a recommended way to check a type against a bound while preventing type erasure?





Aucun commentaire:

Enregistrer un commentaire