Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manifest[T].erasure is deprecated in 2.10, what should I use now?

I have the following code:

object Log {

  def get[T](implicit manifest : Manifest[T] ) = {
    LoggerFactory.getLogger( manifest.erasure.getName )
  }

  def getByName( name : String ) = {
    LoggerFactory.getLogger(name)
  }

}

The idea is to use it like this:

object SimpleFuture {
  val log = Log.get[SimpleFuture[Throwable,Nothing]]
}

But the compiler (2.10) now says that manifest.erasure is deprecated. What should I use now for this same functionality?

like image 569
Maurício Linhares Avatar asked Mar 19 '13 02:03

Maurício Linhares


2 Answers

The simplest fix is to replace the call to erasure with a call to runtimeClass. If you run with -deprecation, the compiler actually gives that recommendation:

warning: method erasure in trait ClassManifestDeprecatedApis is deprecated: Use runtimeClass instead

Alternatively, you can use a class tag:

def get[T](implicit tag : reflect.ClassTag[T] ) = {
  LoggerFactory.getLogger( tag.runtimeClass.getName )
}

This alternative is more future proof than using Manifest. Manifest is due for deprecation in 2.10.something or soon after. See the "Type Tags and Manifests" section of this document for more info.

like image 82
mpilquist Avatar answered Oct 22 '22 16:10

mpilquist


If, like me, you're only interested in avoiding deprecation warnings then you can also use manifest.runtimeClass.getName (instead of manifest.erasure.getName).

IE:

def classOp[T](implicit manifest: Manifest[T]) {
  println("Class: " + manifest.runtimeClass.getName)
}

I've also found another technique which I don't fully understand and may or may not be useful...

def classOp[T <: Any : Manifest] {
  println("Class: " + manifest[T].runtimeClass.getName)
}
like image 3
Peter L Avatar answered Oct 22 '22 15:10

Peter L