简体   繁体   中英

Scalaz === on Some

Difficulty in understanding the behaviour of === in 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.

In (3) I had to cast Some to Option to get the result.

I looked at the source code of scalaz but couldn't get it.

Please explain.

Scalaz Version = 7.1.0

Scala Version = 2.11.2

Some(1) returns Some[Int] rather than Option[Int] ; the whole point of .some is to return Option[Int] instead to be more compatible with the scalaz style. 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM