我有一个具有自定义特征的类型定义:
trait Foo {
def bar: String
}
val BooleanType: Class[Boolean] with Foo = classOf[Boolean] with Foo {
def bar = ""
}
//Error: error: ';' expected but 'with' found.
但是,这不起作用。我也尝试过:
val BooleanType: Class[Boolean] with Foo = classOf[Boolean] {...}
//Error: Class[Boolean](classOf[scala.Boolean]) does not take parameters
和
val BooleanType: Class[Boolean] with Foo = classOf[Boolean] with new Foo {...}
//Error: error: ';' expected but 'with' found.
均无效。如何实现?
编辑:我也尝试使用scala类型:
val Boolean: Type with Foo = typeOf[Boolean] with Foo {...}
//error: ';' expected but 'with' found.
答案 0 :(得分:1)
您无法创建Class
的子类,因为它是最终类。如果可以,并且如果有构造函数,则语法为:
val BooleanType: Class[Boolean] with Foo = new Class[Boolean](< args >) with Foo {
def bar = ""
}
据我所知,不可能对特定实例进行子类化(就像您尝试使用classOf[Boolean]
一样)。