【发布时间】:2021-02-02 15:11:40
【问题描述】:
我很好奇是否可以将我的无类型项目移植到使用 Scala 3 进行类型化。 这是我的开始:
object Main {
type HtmlNodeRecord[X]= X match {
case "tag" => String
case "attrs" => List[(String, String)]
case "children" => List[HtmlNode]
}
case class HtmlNode(tag: String, attrs: List[(String, String)], children: List[HtmlNode]) {
def apply(s: "tag" | "attrs" | "children"): HtmlNodeRecord[s.type] = s match {
case "tag" => tag
case "attrs" => attrs
case "children" => children
}
}
}
不编译,报错:
> [E007] Type Mismatch Error: Main.scala:10:22
> [error] 10 | case "tag" => tag
> [error] | ^^^
> [error] | Found: (HtmlNode.this.tag : String)
> [error] | Required: Main.HtmlNodeRecord[
> [error] | (s : ("tag" : String) | ("attrs" : String) | ("children" : String))
> [error] | ]
我认为这是因为它没有将模式匹配视为 s 的“类型过滤器”,因为它认为在这种情况下,s 的类型为 "tag" | "attrs" | "children",而模式匹配的情况应该将其简化为“标签”。
如何实现我请求的行为?
【问题讨论】:
标签: scala scala-3 singleton-type dotty match-types