如何在Scala中使用Manumest with Enumeration?

时间:2011-04-17 21:09:19

标签: scala manifest enumeration

如果我有以下Scala代码:

trait BaseTrait[EnumType <: Enumeration] {
    protected val enum: EnumType
    protected val valueManifest: Manifest[EnumType#Value]
}

object MyEnum extends Enumeration {
    val Tag1, Tag2 = Value
}

我想创建一个使用MyEnum实现BaseTrait的类,我可以这样做:

class BaseClass[EnumType <: Enumeration]
(protected val enum: EnumType)
(implicit protected val valueManifest: Manifest[EnumType#Value])
extends BaseTrait[EnumType] {
}

class Test extends BaseClass(MyEnum)

但是如果没有中间基类怎么能这样做呢?所有其他尝试总是导致编译错误。

1 个答案:

答案 0 :(得分:1)

你没有写下你尝试的内容,但我的猜测是你的课程扩展BaseTrait[MyEnum]。由于MyEnumobject,因此MyEnum类型不存在(除非您还定义了具有该名称的类或特征)。

您必须明确提供单例类型MyEnum.type作为类型参数。

class Test extends BaseTrait[MyEnum.type] {
  protected val enum = MyEnum
  protected val valueManifest = manifest[MyEnum.type#Value]
}