Scala 引用路径依赖类型的子类型
以下工作:Scala 引用路径依赖类型的子类型,scala,path-dependent-type,Scala,Path Dependent Type,以下工作: class Outter { type Inner = Either[Int,String] type L = Left[Int,String] type R = Right[Int,String] def f(x: Inner) = 1 } val o = new Outter o.f(new o.L(1)) o.f(new o.R("name")) 但这仅仅是因为内部的所有子类型都有一个显式的类型成员。是否可以从路径依赖类型的
class Outter {
type Inner = Either[Int,String]
type L = Left[Int,String]
type R = Right[Int,String]
def f(x: Inner) = 1
}
val o = new Outter
o.f(new o.L(1))
o.f(new o.R("name"))
但这仅仅是因为内部的所有子类型都有一个显式的类型成员。是否可以从路径依赖类型的子类型构造值,而无需在Outter
中明确提及它们?比如:
class Outter {
type Inner = Either[Int,String]
def f(x: Inner) = 1
}
val o = new Outter
o.f(new o.?!?(1)) // How do I express "that particular Left[Int,String] which is the sub-type of o.Inner
o.f(new o.?!?("name")) // same as above here, but for Right
相关的
这是一个类型别名internal
不是[Int,String]
的子类,它们是相同的。只是语法糖。
因此,您仍然可以引用的子类[Int,String]
,就好像它们是内部的子类一样
因此,解决方案可能比您想象的更简单
val o = new Outter
o.f(Left(1)) // How do I express "that particular Left[Int,String] which is the sub-type of o.Inner
o.f(Right("name")) // same as above here, but for Right
“这是一个类型别名。other[Int,String]
不是other
的子类。”是的,我知道。这不是我的问题(顺便说一句,other[Int,String]
与other
不一样。前者是正确的类型,后者是arity 2的类型构造函数)。问题是,我如何创建预期类型的Left
和Right
(即o.Inner
而不是Inner
)。我手头没有REPL,但我认为您的代码没有进行类型检查(类型不匹配,必需o.internal
,找到Left
)“顺便说一下,other[Int,String]
与other
”不一样。这是我的错别字。我的意思是“internal
也不是[Int,String]
的子类”。我编辑了我的答案。显然你是对的!我将做一点实验,然后回来。请看相关的问题(我在这篇文章的末尾添加了它)
val o = new Outter
o.f(Left(1)) // How do I express "that particular Left[Int,String] which is the sub-type of o.Inner
o.f(Right("name")) // same as above here, but for Right