[英]Scalaz === on Some
Difficulty in understanding the behaviour of === in scalaz 难以理解scalaz中===的行为
1. scala> 1.some === Some(1)
res33: Boolean = true
2. scala> Some(1) === 1.some
<console>:14: error: value === is not a member of Some[Int]
Some(1) === 1.some
^
3. scala> (Some(1):Option[Int]) === 1.some
res35: Boolean = true
I can understand (1) and I expect even (2) to give same result. 我可以理解(1)并且我希望偶数(2)给出相同的结果。
In (3) I had to cast Some to Option to get the result. 在(3)中,我必须将Some转换为Option以获得结果。
I looked at the source code of scalaz but couldn't get it. 我查看了scalaz的源代码,但无法得到它。
Please explain. 请解释。
Scalaz Version = 7.1.0 Scalaz版本= 7.1.0
Scala Version = 2.11.2 Scala版本= 2.11.2
Some(1)
returns Some[Int]
rather than Option[Int]
; Some(1)
返回Some[Int]
而不是Option[Int]
; the whole point of .some
is to return Option[Int]
instead to be more compatible with the scalaz style. .some
是返回Option[Int]
而不是与scalaz样式更兼容。 Scalaz is written for a non-subtyping ADT-based programming style and most scalaz typeclasses have very little support for subtyping (in particular, they're usually invariant; often this improves type inference and means better error messages when users make mistakes, but at the cost of poorer support for subtypes). Scalaz是针对非子类型的基于ADT的编程风格编写的,大多数scalaz类型类很少支持子类型(特别是它们通常是不变的;通常这会改善类型推断,并且当用户出错时意味着更好的错误消息,但是支持亚型的成本较低)。 If you're making heavy use of scalaz, you will probably find it easiest to use scalaz-style "smart constructors" like .some
all the time. 如果你正在大量使用scalaz,你可能会发现最容易使用像.some
一样的.some
风格的“智能构造函数”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.