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?
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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With