Summary of the Haskell these package :
The "These" type represents values with two non-exclusive capabilities.
data These a b = This a | That b | These a b
Is there anything similar in Scala? Maybe in a scalp?
For those new to Haskell, here's a rough sketch of how to 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 can do something like join Eithers:
type These[A, B] = Either[Either[A, B], (A, B)]
(Obviously, expressing the data structure is not complicated. But if there is something already thought out in the existing library, I would rather just use it.)
source
share