Scala 高级类型与函数式编程深度解析
1. 中缀类型(Infix Types)
在 Scala 里,接收两个类型参数的类型能够采用中缀表示法来书写。以 Either[A, B] 为例:
val left1: Either[String,Int] = Left("hello")
val left2: String Either Int = Left("hello")
val right1: Either[String,Int] = Right(1)
val right2: String Either Int = Right(2)
中缀类型可以嵌套,默认是左结合的,不过若类型名以冒号 : 结尾,则为右结合,这和普通表达式的规则一致。可以借助括号来改变默认的结合性:
// src/main/scala/progscala2/typesystem/valuetypes/infix-types.sc
scala> val xll1: Int Either Double Either String = Left(Left(1))
xll1: Either[Either[Int,Double],String] = Left(Left(1))
scala> val xll2: (Int Either Double) Either String = Left(Left(1))
xll2: Either[Either[Int,Double],String
超级会员免费看
订阅专栏 解锁全文
53

被折叠的 条评论
为什么被折叠?



