类型参数中`::`的含义?
Meaning of `::` in Type Parameter?
查看 Travis Brown 在 Type classes and generic derivation 上的优秀博客 post,我看到了以下方法:
implicit def hconsParser[H: Parser, T <: HList: Parser]: Parser[H :: T] =
new Parser[H :: T] {
def apply(s: String): Option[H :: T] = s.split(",").toList match {
case cell +: rest => for {
head <- implicitly[Parser[H]].apply(cell)
tail <- implicitly[Parser[T]].apply(rest.mkString(","))
} yield head :: tail
}
}
Parser[H :: T]
中的H :: T
是什么意思?
此外,case cell +: rest
如何处理 s
的情况,即 apply
的输入为空?
H :: T
是类型 ::[H, T]
的中缀形式,它是一个 HList
头部类型 H
尾部类型 T <: HList
.即,我们正在寻找 ::[H, T]
类型的 Parser
。
中缀用法是这样实现的,其中infix
可以是任意名称:
scala> trait infix[A, B]
scala> def test[A, B](ab: A infix B) = ???
test: [A, B](ab: infix[A,B])Nothing
Also, how does this case cell +: rest
handle the case where s
, i.e. input to apply
is empty?
如果 s
是一个空字符串,那么 s.split(",").toList
将只是一个 List
,其中一个空字符串作为其单个元素。 case cell +: rest
永远不会 运行 进入空列表。
查看 Travis Brown 在 Type classes and generic derivation 上的优秀博客 post,我看到了以下方法:
implicit def hconsParser[H: Parser, T <: HList: Parser]: Parser[H :: T] =
new Parser[H :: T] {
def apply(s: String): Option[H :: T] = s.split(",").toList match {
case cell +: rest => for {
head <- implicitly[Parser[H]].apply(cell)
tail <- implicitly[Parser[T]].apply(rest.mkString(","))
} yield head :: tail
}
}
Parser[H :: T]
中的H :: T
是什么意思?
此外,case cell +: rest
如何处理 s
的情况,即 apply
的输入为空?
H :: T
是类型 ::[H, T]
的中缀形式,它是一个 HList
头部类型 H
尾部类型 T <: HList
.即,我们正在寻找 ::[H, T]
类型的 Parser
。
中缀用法是这样实现的,其中infix
可以是任意名称:
scala> trait infix[A, B]
scala> def test[A, B](ab: A infix B) = ???
test: [A, B](ab: infix[A,B])Nothing
Also, how does this
case cell +: rest
handle the case wheres
, i.e. input toapply
is empty?
如果 s
是一个空字符串,那么 s.split(",").toList
将只是一个 List
,其中一个空字符串作为其单个元素。 case cell +: rest
永远不会 运行 进入空列表。