【发布时间】:2011-11-09 19:41:29
【问题描述】:
考虑这段代码:
class Foo[T : Manifest](val id: String = manifest[T].erasure.getName)
我基本上想在 Foo 中存储一个标识符,通常只是类名。
不需要特殊标识符的子类可以轻松使用默认值。
但这甚至没有编译,错误信息是:
error: No Manifest available for T.
还有其他可行的方法吗?
编辑:
如果清单在主构造函数之前不可用,为什么这会起作用?
class Foo[T: Manifest](val name: String) {
def this() = this(manifest[T].erasure.getName)
}
【问题讨论】:
-
不是答案,但如果您使用
manifest[T]而不是implicitly[Manifest[T]],您将拥有更简洁的代码。
标签: class scala types constructor manifest