[英]How to declare a method which may return 3 different kinds of type?
I want to define a method, but it's return type has 3 kinds. 我想定义一个方法,但是它的返回类型有3种。
def findSelectedItem: ??? = { ... }
The ???
???
here may be Category
, Section
, Page
, but I'm not sure how to find a proper type to represent it. 这里可能是Category
, Section
, Page
,但是我不确定如何找到合适的类型来表示它。
If it's just 2, I can use Either[Type1, Type2]
, but it's 3 now. 如果只有2,则可以使用Either[Type1, Type2]
,但现在为3。
Do I need to declare something like Either
but which has 3 type variables? 我是否需要声明类似Either
但具有3个类型变量的内容? Or is there already something I can just use? 还是已经有我可以使用的东西了?
You could nest Either
: 您可以嵌套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
: 或者,创建自己的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 { 对象ItemFinder {
def apply(s: String):Item = s match{
case Category.name => Category
case Section.name => Section
case Page.name => Page
} } }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.