加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – Manifest [T] .erasure在2.10中被弃用,我现在应该使用

发布时间:2020-12-16 09:00:45 所属栏目:安全 来源:网络整理
导读:我有以下代码: object Log { def get[T](implicit manifest : Manifest[T] ) = { LoggerFactory.getLogger( manifest.erasure.getName ) } def getByName( name : String ) = { LoggerFactory.getLogger(name) }} 想法是这样使用它: object SimpleFuture {
我有以下代码:

object Log {

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

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

}

想法是这样使用它:

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

但是编译器(2.10)现在说manifest.erasure已被弃用.我现在应该使用什么来实现相同的功能?

解决方法

最简单的解决方法是通过调用runtimeClass来替换对擦除的调用.如果使用-deprecation运行,编译器实际上会给出该建议:

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

或者,您可以使用类标记:

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

这种替代方案比使用Manifest更具未来性.清单应在2.10.something或之后不久弃用.有关详细信息,请参阅this document的“类型标记和清单”部分.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读