Skip to content

Instantly share code, notes, and snippets.

@chrislewis chrislewis/OneOf3.scala
Last active Jan 11, 2017

Embed
What would you like to do?
/** A boxed union of 3 types; same principle applies for > 3. */
sealed trait OneOf3[A, B, C] {
/** There are plenty of useful methods we could define, but folds provides
an alternative to explicit pattern matching and a basis for other
useful things. */
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X
}
/* Implementations are straight forward: */
final case class FirstOf3[A, B, C](a: A) extends OneOf3[A, B, C] {
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X = fax(a)
}
final case class SecondOf3[A, B, C](b: B) extends OneOf3[A, B, C] {
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X = fbx(b)
}
final case class ThirdOf3[A, B, C](c: C) extends OneOf3[A, B, C] {
def fold[X](fax: A => X, fbx: B => X, fcx: C => X): X = fcx(c)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.