Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing deprecated <:< Manifest type witness in Scala 2.10

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

like image 621
Josh Marcus Avatar asked Dec 11 '12 19:12

Josh Marcus


1 Answers

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.

like image 68
Travis Brown Avatar answered Nov 15 '22 20:11

Travis Brown