scala – 类型参数中`::`的含义?
发布时间:2020-12-16 18:19:01 所属栏目:安全 来源:网络整理
导读:看看Travis Brown在 Type classes and generic derivation的优秀博客文章,我看到以下方法: 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 m
看看Travis Brown在
Type classes and generic derivation的优秀博客文章,我看到以下方法:
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 } } 在解析器[H :: T]中H :: T的含义是什么? 另外,这个case cell:rest处理s的情况,即要应用的输入是空的? 解决方法
H :: T是类型:: [H,T]的中缀形式,它是具有类型H的头部和具有类型T<:HList的尾部的HList.即我们正在寻找类型:: [H,T]的解析器. 中缀的使用是这样实现的,其中的中缀可以是任何名称:
scala> trait infix[A,B] scala> def test[A,B](ab: A infix B) = ??? test: [A,B](ab: infix[A,B])Nothing
如果s是一个空字符串,则s.split(“,”).toList将只是一个以空字符串作为其单个元素的List. case cell:rest会永远不会遇到空列表. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |