Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 引用路径依赖类型的子类型_Scala_Path Dependent Type - Fatal编程技术网

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