9

Can someone point me at what I should be doing under scala 2.10 in place of this deprecated type witness on Manifest?

reflect.ClassManifest.singleType(foo) <:< barManifest

Honestly, my goal here is just to replace it with something that doesn't raise a deprecation warning. I'm happy to use the new reflection API.

Here's the code in question in context, if that's important:

https://github.com/azavea/geotrellis/blob/master/src/main/scala/geotrellis/feature/op/geometry/geometry.scala#L45

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Josh Marcus
  • 1,749
  • 18
  • 30

1 Answers1

10

If you want a fairly literal translation from manifests to type tags, you'll need to get the appropriate mirror, use it to reflect your instance, and then use the <:< on Type. For example:

import scala.reflect.runtime.currentMirror
import scala.reflect.runtime.universe._

sealed trait X
case class Y(i: Int) extends X
case class Z(j: String) extends X

def filterX[A <: X: TypeTag](xs: List[X]) = xs.filter(
  x => currentMirror.reflect(x).symbol.toType <:< typeOf[A]
)

And now:

scala> filterX[Z](List(Y(1), Y(2), Z("test")))
res1: List[X] = List(Z(test))

scala> filterX[Y](List(Y(1), Y(2), Z("test")))
res2: List[X] = List(Y(1), Y(2))

There may be ways you could take advantage of the new Reflection API more fully in your application, but this should work and will take care of the deprecation warnings.

Travis Brown
  • 138,631
  • 12
  • 375
  • 680
  • Very helpful answer! But when I actually attempt this in my project, I get the following runtime error: illegal cyclic reference involving class LineString (at scala.reflect.internal.Symbols$TypeSymbol.tpe(Symbols.scala:2710)) Of course, that's probably just related to the specific classes in question. – Josh Marcus Dec 12 '12 at 15:14