相关疑难解决方法(0)

抽象类型模式是未选中的,因为它是通过擦除来消除的

有人可以告诉我如何避免下面的代码块中的警告:

abstract class Foo[T <: Bar]{
  case class CaseClass[T <: Bar](t: T)
  def method1 = {
    case CaseClass(t: T) => println(t)
    csse _ => 
  }
}
Run Code Online (Sandbox Code Playgroud)

这会导致编译器警告:

 abstract type pattern T is unchecked since it is eliminated by erasure
 case CaseClass(t: T) => println(t)
                   ^
Run Code Online (Sandbox Code Playgroud)

scala

24
推荐指数
1
解决办法
7684
查看次数

如何知道对象是否是TypeTag类型的实例?

我有一个函数,它能够知道一个对象是否是一个Manifest类型的实例.我想将它迁移到一个TypeTag版本.旧功能如下:

def myIsInstanceOf[T: Manifest](that: Any) = 
  implicitly[Manifest[T]].erasure.isInstance(that)
Run Code Online (Sandbox Code Playgroud)

我一直在试验TypeTags,现在我有了这个TypeTag版本:

// Involved definitions
def myInstanceToTpe[T: TypeTag](x: T) = typeOf[T]
def myIsInstanceOf[T: TypeTag, U: TypeTag](tag: TypeTag[T], that: U) = 
  myInstanceToTpe(that) stat_<:< tag.tpe

// Some invocation examples
class A
class B extends A
class C

myIsInstanceOf(typeTag[A], new A)        /* true */
myIsInstanceOf(typeTag[A], new B)        /* true */
myIsInstanceOf(typeTag[A], new C)        /* false */
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来完成这项任务?参数化是否U可以省略,使用Any替代(就像在旧函数中一样)?

scala instanceof scala-2.10

19
推荐指数
2
解决办法
7504
查看次数

标签 统计

scala ×2

instanceof ×1

scala-2.10 ×1