I want to define a method, but it's return type has 3 kinds.
def findSelectedItem: ??? = { ... }
The ???
here may be Category
, Section
, Page
, but I'm not sure how to find a proper type to represent it.
If it's just 2, I can use Either[Type1, Type2]
, but it's 3 now.
Do I need to declare something like Either
but which has 3 type variables? Or is there already something I can just use?
You could nest Either
:
Given
case class Category(name: String)
case class Section(name: String)
case class Page(name: String)
and a method like this:
def f(name: String): Either[Category, Either[Section, Page]] = {
name match {
case "c" =>
Left(Category(name))
case "s" =>
Right(Left(Section(name)))
case "p" =>
Right(Right(Page(name)))
}
}
then you can "pattern match" on the result:
Seq("c", "s", "p").map(f).foreach {
case Left(c) => println("C")
case Right(Left(s)) => println("S")
case Right(Right(p)) => println("p")
}
As an alternative, create your own Either3
:
case class Either3[+A, +B, +C](left: Option[A], middle: Option[B],
right: Option[C])
object Left3 {
def apply[A, B, C](a: A): Either3[A, B, C] = {
Either3(Some(a), None, None)
}
def unapply[A, B, C](e: Either3[A, B, C]): Option[A] = {
e.left
}
}
object Middle3 {
def apply[A, B, C](b: B): Either3[A, B, C] = {
Either3(None, Some(b), None)
}
def unapply[A, B, C](e: Either3[A, B, C]): Option[B] = {
e.middle
}
}
object Right3 {
def apply[A, B, C](c: C): Either3[A, B, C] = {
Either3(None, None, Some(c))
}
def unapply[A, B, C](e: Either3[A, B, C]): Option[C] = {
e.right
}
}
and then
def f(name: String): Either3[Category, Section, Page] = {
name match {
case "c" =>
Left3(Category(name))
case "s" =>
Middle3(Section(name))
case "p" =>
Right3(Page(name))
}
}
together with
Seq("c", "s", "p").map(f).foreach {
case Left3(c) => println("C")
case Middle3(s) => println("S")
case Right3(p) => println("p")
}
You could also use an abstract class:
abstract sealed class Item(val name:String)
case object Category extends Item("category")
case object Section extends Item("sections")
case object Page extends Item("page")
object ItemFinder {
def apply(s: String):Item = s match{
case Category.name => Category
case Section.name => Section
case Page.name => Page
} }
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.