仍在挣扎着使用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)
是为了便于理解。
编辑
作为一个澄清,与参考答案“FYI,并关闭这个问题”线程Constraining an operation by matching a type parameter to an argument's path-dependent type - 我想有除了是创建对象的可能性(参考)不使用工厂方法在访问但在某处以外(例如与new
语句)。因为系统不能被Access的定义所限制,所以我必须能够用更多的对象来扩展它。
我又增加了一个问题:http://stackoverflow.com/questions/5575030/driving-a-singleton-type - 我认为如果我能解决这个问题,这个问题也将因此而得到解决(我希望) – 2011-04-07 01:52:30