一般获取 Scala 类型的清单
有没有办法通用地获取 Scala 类型的清单?例如,如果我只有一个类引用可以使用,有没有办法获得其相应的清单?
Is there a way to generically obtain a Scala type's manifest? For example, if I only have a Class reference to work with, is there a way I can obtain its corresponding manifest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Scala 的内置
implicitly[T]
将为您提供 T 类型的隐式值(如果存在)。因此,隐式[Manifest[T]]
将返回类型T的Manifest。如果您有Class引用,则可以像这样获取该类的Manifest:
Manifest的类型为
_ < ;: Foo
而不仅仅是Foo
,但它们是相等的。Scala's built-in
implicitly[T]
will get you an implicit value of type T (if one exists). Soimplicitly[Manifest[T]]
will return a Manifest for type T.If you have a Class reference, you can get the Manifest for the Class like this:
The Manifest's type is
_ <: Foo
instead of justFoo
, but they are equal.嗯,我试图做一些类似的事情,但经过反思。我在编译时没有可用的类符号。我必须使用这段代码,从 scaladocs 来看并不是那么明显,在该点附近存在多种非显而易见和误导性的方法,因此它可能对其他人有用。
Well, I was trying to do something similar, but reflectively. I had no class symbol at compile time available. I had to use this code, not so obvious from the scaladocs, whith multiple non-obvious and misleading methods present near that point, so that it would likely be useful to others out there.