繁体   English   中英

如何声明一个可以返回3种不同类型的方法?

[英]How to declare a method which may return 3 different kinds of type?

我想定义一个方法,但是它的返回类型有3种。

def findSelectedItem: ??? = { ... }

??? 这里可能是CategorySectionPage ,但是我不确定如何找到合适的类型来表示它。

如果只有2,则可以使用Either[Type1, Type2] ,但现在为3。

我是否需要声明类似Either但具有3个类型变量的内容? 还是已经有我可以使用的东西了?

您可以嵌套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")
}

您还可以使用抽象类:

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

}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM