我想定义一个方法,但它的返回类型有3种。
def findSelectedItem: ??? = { ... }
此处的???
可能是Category
,Section
,Page
,但我不确定如何找到合适的类型来代表它。
如果它只是2,我可以使用Either[Type1, Type2]
,但现在是3。
我是否需要声明类似Either
但有3个类型变量的内容?或者我已经可以使用它了吗?
答案 0 :(得分:5)
您可以嵌套Either
:
鉴于
case class Category(name: String)
case class Section(name: String)
case class Page(name: String)
和这样的方法:
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)))
}
}
然后你可以"模式匹配"结果:
Seq("c", "s", "p").map(f).foreach {
case Left(c) => println("C")
case Right(Left(s)) => println("S")
case Right(Right(p)) => println("p")
}
作为替代方案,请创建自己的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
}
}
然后
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))
}
}
与
一起Seq("c", "s", "p").map(f).foreach {
case Left3(c) => println("C")
case Middle3(s) => println("S")
case Right3(p) => println("p")
}
答案 1 :(得分:-1)
您还可以使用抽象类:
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")
对象ItemFinder {
def apply(s: String):Item = s match{
case Category.name => Category
case Section.name => Section
case Page.name => Page
} }