定义一个方法,其返回类型是该方法参数的单例类型
仍在与 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)
。
编辑
作为澄清,参考线程通过将类型参数与参数的路径相关类型匹配来约束操作——我还想要什么可以不使用 Access 中的工厂方法,而是使用外部的某个地方(例如使用 new 语句)来创建对象 (Refs)。因为系统不能受到 Access 定义的限制,所以我必须能够使用更多对象来扩展它。
Still struggling with this.types (singleton types). Assume this scenario:
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
}
The following fails:
def test(sys: Sys[Access]): Unit =
sys.in("v1") { implicit a =>
val r = Ref.single(44)
a.set(r)
}
because apparently r
is of type Ref[Access, Int]
and not Ref[a.type, Int]
. My guess is the problem is that I would need a line like
def single[A <: Access, V](v: V)(implicit a: A): Ref[a.type, V] = ...
which isn't compiling as due to "illegal dependent method type"...
Any ideas how I can fix this. The demand is that I do not explicitly annotate calls with types. That is, I do not want to write Ref.single[a.type, Int](44)(a)
for comprehensible reasons.
EDIT
As a clarification, with reference to answer "FYI, and to close the question" in thread Constraining an operation by matching a type parameter to an argument's path-dependent type -- what I would like to have in addition is the possibility to create objects (Refs) not by using a factory method in the Access but somewhere outside (e.g. with a new
statement). Because the system cannot be limited by the definition of Access, I must be able to extend it with further objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有几种可能性。使用 Scala 2.8/2.8.1,您可以使用私有选项 -Ydependent-method-types ,然后您的解决方案可以
正常编译。
如果您想避免依赖方法类型,因为它是私有选项,您仍然可以通过显式键入对 Ref.single 的调用来编译您的第一个提案:
不过,您需要将类型指定为单例类型永远不会被推断。您的问题与未推断单例类型的问题不同,但与之相关:请参阅 如何正确键入注释此 HList?
You have several possibilities. With Scala 2.8/2.8.1, you can use the private option
-Ydependent-method-types
and then your solution withcompiles fine.
If you want to avoid dependent method types because it's a private option, you can still make your first proposal compile by explicitly typing the call to
Ref.single
:You need to specify the type, though, as singleton types are never inferred. You problem is not the same as, but related to, the problem that singleton types are not inferred: see How to correctly type-annotate this HList?