我正在与Scala 2.11.7中的依赖类型进行斗争 . 以下是上下文:
trait Counter {
type T
def zero: T
def incr( t: T ): T
}
object IntCounter extends Counter {
type T = Int
val zero = 0
def incr( t: Int ) = t + 1
}
case class Foo( counter: Counter )
def twice( foo: Foo )( cntr: foo.counter.T ): foo.counter.T =
foo.counter.incr( foo.counter.incr( cntr ) )
到目前为止一切都那么好,一切都汇编了但是我想添加一个包含 Foo
实例和相应计数器状态的对象 . 例如:
trait Bar {
val foo: Foo
val current: foo.counter.T
}
定义没问题(前提是我使用抽象vals) . 但是我无法定义工厂方法(也就是智能构造函数) . 我所有天真的尝试都无法编译 . 例如,定义:
def bar( f: Foo )( cntr: f.counter.T ): Bar = new Bar {
val foo = f
val current = cntr
}
无法编译错误:
xxx: overriding value current in trait Bar of type this.foo.counter.T;
value current has incompatible type
val current = cntr
^
如何强制编译器理解这两种类型确实相同?我可以用泛型来解决问题,但如果可能的话,我更愿意避免使用此选项 .
1 回答
如果
bar
是Bar
的单个构造函数,您可以像这样解决它:用法:
这里的缺点是,无论在哪里创建新的
Foo
实例,都必须在TT
和foo
成员之间指定(并维护)相同的根类型 .