【发布时间】:2011-04-07 00:57:00
【问题描述】:
仍在为 this.types(单例类型)而苦苦挣扎。假设这种情况:
trait Sys[A <: Access] {
def in[T](v: String): AccessPrepare[A]
}
trait AccessPrepare[A <: Access] {
val a: A
def apply[T](fun: a.type => T): T
}
object Ref {
def single[A <: Access, V](v: V)(implicit a: A): Ref[A, V] = ???
}
trait Ref[A, V]
trait Access {
def set(r: Ref[this.type, Int]): Unit
}
以下失败:
def test(sys: Sys[Access]): Unit =
sys.in("v1") { implicit a =>
val r = Ref.single(44)
a.set(r)
}
因为显然r 的类型是Ref[Access, Int] 而不是Ref[a.type, Int]。我的猜测是问题是我需要像这样的一行
def single[A <: Access, V](v: V)(implicit a: A): Ref[a.type, V] = ...
由于“非法依赖方法类型”而无法编译...
任何想法我可以解决这个问题。要求是我没有用类型明确地注释调用。也就是说,出于可以理解的原因,我确实不想写Ref.single[a.type, Int](44)(a)。
编辑
作为澄清,参考线程 Constraining an operation by matching a type parameter to an argument's path-dependent type 中的回答“仅供参考,并关闭问题”——我还想拥有不使用工厂方法创建对象(Refs)的可能性在访问但在外部某处(例如,使用new 语句)。因为系统不受 Access 定义的限制,所以我必须能够用更多的对象来扩展它。
【问题讨论】:
-
我又加了一个问题:stackoverflow.com/questions/5575030/…——我想如果我能解决那个问题,这个问题也将得到解决(我希望)