简体   繁体   English

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

[英]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. 这里可能是CategorySectionPage ,但是我不确定如何找到合适的类型来表示它。

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.

相关问题 如何声明从另一个方法的返回类型引用的变量/方法类型 - How to declare variable/method type referred from the return type of another method 如何模拟没有返回值的方法是Option [SomeCaseClassDefinedInsideThisClass]的方法的返回 - How to mock return of None of method which return type is Option[SomeCaseClassDefinedInsideThisClass] 如何设计不同种类的(几乎)相同的(在 Scala 中) - How to design different kinds of (nearly) the same (in Scala) 在对象上进行模式匹配时可以克服Scala中的类型擦除,这些对象可能是不同的Set或任何类型的Object - Overcoming type erasure in Scala when pattern matching on Objects which may be different Sets or any type of Object 是否可以声明关闭的返回类型? - Is it possible to declare return type for closure? 类型理论:类型种类 - Type theory: type kinds 在不同对象上调用相同方法时,返回类型不同 - Return type is different when invoking the same method on different objects 种类不符合lambda类型 - Kinds not conforming with type lambda 使用返回不同数值类型值的方法定义特征 - Defining a trait with a method that return a value of different numeric type 如何在scala中为方法返回不同的未来类型 - How to return different future types in scala for a method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM