如何在Scala中与枚举一起使用清单?

如何在Scala中与枚举一起使用清单?,scala,manifest,enumeration,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 } trait-BaseTrait[EnumType您没有编写

如果我有以下Scala代码:

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

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

trait-BaseTrait[EnumType您没有编写您尝试编写的内容,但我猜您让您的类扩展了
BaseTrait[MyEnum]
。因为
MyEnum
是一个
对象
类型
MyEnum
不存在(除非您还用该名称定义了一个类或trait)

您必须显式地提供singleton类型
MyEnum.type
作为类型参数

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

为什么不使用
抽象类
而不是
BaseTrait
?这个trait来自一个API。我自己定义了枚举和实现这个trait的类。
class Test extends BaseTrait[MyEnum.type] {
  protected val enum = MyEnum
  protected val valueManifest = manifest[MyEnum.type#Value]
}