Summary of the Haskell these package:
The 'These' type represents values with two non-exclusive possibilities
data These a b = This a | That b | These a b
Is there anything similar in Scala? Maybe in scalaz?
For those unfamiliar with Haskell, here's a rough sketch of how one might approach this in Scala:
sealed trait These[+A, +B] {
def thisOption: Option[A]
def thatOption: Option[B]
}
trait ThisLike[+A] {
def `this`: A
def thisOption = Some(a)
}
trait ThatLike[+B] {
def `that`: B
def thatOption = Some(b)
}
case class This[+A](`this`: A) extends These[A, Nothing] with ThisLike[A] {
def thatOption = None
}
case class That[+B](`that`: B) extends These[Nothing, B] with ThatLike[B] {
def thisOption = None
}
case class Both[+A, +B](`this`: A, `that`: B) extends These[A, B]
with ThisLike[A] with ThatLike[B]
Or you could do something like combining Either
s:
type These[A, B] = Either[Either[A, B], (A, B)]
(Obviously, expressing the data structure is not difficult. But if there's something already well thought-out in an existing in a library, I'd prefer to just use it.)
scalaz has these, which is also referred to as \\&/
https://github.com/scalaz/scalaz/blob/series/7.2.x/core/src/main/scala/scalaz/These.scala
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.