English 中文(简体)
Scala——从普通类中获取一个等级物体
原标题:Scala - obtaining a class object from a generic type

是否可纯粹从通用参数中产生一类物体? 例如:

class myclass[T] { 
  def something(): Class[_ <: T] = 
    classOf[T] //this doesn t work
}

Since the type will have been erased at runtime, it seems like this a job for manifests, but I haven t found an example that demonstrates this particular usage. I tried the following, but it doesn t work either:

class myclass[T] { 
  def something()(implicit m: Manifest[T]): Class[_ <: T] = 
    m.erasure //this doesn t work
}

我怀疑这一失败是因为,正如APICS指出的那样,在<代码>m.erasure的类型之间不存在亚类关系。 页: 1 T。

EDIT: I m not really interested in what the type T is, I just need an object of type Class[_ <: T] to pass to a method in the hadoop framework.

任何要点?

最佳回答

You can cast the result of m.erasure to a Class[T]:

class myclass[T] { 
    def something()(implicit m: Manifest[T]): Class[T] = 
        m.erasure.asInstanceOf[Class[T]]
}

This works fine for basic (non-generic) types:

scala> new myclass[String]().something()
res5: Class[String] = class java.lang.String

但是,我注意到如果使用一个即时型构造,例如<代码>List[String],:

scala> new myclass[List[String]]().something()
res6: Class[List[String]] = class scala.collection.immutable.List

由于舱位,只有一张<代码>Clas的物体,用于特定类型构造的所有可能的瞬时发射。

Edit

I m不肯定为什么Manifest[T].erasure 回归代码,而不是Class[T],但如果我不得不猜测,我要说,这不利于你使用上的方法。 职等允许你比较两个平等或亚类关系类别,因为这些方法将在<代码>Class<>/code>与瞬时通用类型成参数时给你错误答案。

例如,

scala> classOf[List[String]] == classOf[List[Int]]
res25: Boolean = true

scala> classOf[List[String]].isAssignableFrom(classOf[List[Int]])
res26: Boolean = true

这些结果可能使你感到惊讶,并(或)导致贵方案出现ug。 通常,你不应以这种方式比较班级,而应仅通过<代码>Manifest,而应加以比较,因为它们有更多的信息*:

scala> manifest[List[String]] == manifest[List[Int]]
res27: Boolean = false

scala> manifest[List[String]] >:> manifest[List[Int]]
res28: Boolean = false

如我所知,<代码>Manifests 意在取代大多数使用案例的<编码>Class,但当然,如果你重新使用需要<代码>的“Class的框架,则没有什么选择。 我会相信,强加于人,产生<代码>.sure<>/code>的结果,只是一种“承认责任”,你在自己的风险下重新使用低劣产品:

* E/CN.6/2009/1。 请注意,正如的文件所述,这些明显的比较操作者“只能视为近似值,因为在标识中还没有充分代表的符合类型的许多方面

问题回答
def myClassOf[T:ClassTag] = implicitly[ClassTag[T]].runtimeClass

www.un.org/french/ga/president 如果您想获得全部类型的信息,你就应当回来<>Manifest。

scala> class MyClass[A] {
     |   def stuff(implicit m: Manifest[A]): Class[_] = m.erasure
     | }
defined class MyClass

scala> new MyClass[Int].stuff
res551: java.lang.Class[_] = int

scala> new MyClass[List[Int]].stuff
res552: java.lang.Class[_] = class scala.collection.immutable.List

scala> class MyClass[A] {
     |   def stuff(implicit m: Manifest[A]): Manifest[A] = m
     | }
defined class MyClass

scala> new MyClass[Int].stuff
res553: Manifest[Int] = Int

scala> new MyClass[List[Int]].stuff
res554: Manifest[List[Int]] = scala.collection.immutable.List[Int]




相关问题
Generic constraint exclusion

Sorry for asking silly question Is it possible to enforce constraint on generic in such a way that the given T can be derived from any reference Type except some A,B,C (where A,B,C are reference ...

Can a Type Derive from Itself?

When enforcing a Generic constraint class GenericTest { public void Sample<T>(T someValue) where T:Racer,new() { Console.WriteLine(someValue.Car); } } The Type T should be ...

C# Deriving Generic Methods

When i need to pass a Generic Type I can use the Syntax (Example : Ofcourse it is not a Generic Method) public void Sample(T someValue) { ...... } What is the benefit of declaring Sample<T>...

What is the benefit to using List<T> over IEnumerable<T>?

or the other way around? I use generic lists all the time. But I hear occasionally about IEnumerables, too, and I honestly have no clue (today) what they are for and why I should use them. So, at ...

热门标签