4

我想从函数返回一个类型。例如:

class Super
case class One(a: Int) extends Super
case class Two(b: Float) extends Super
case class Unknown extends Super

def decide(criterion: String): ??? = {
  criterion match {
    case "one" => One
    case "two" => Two
    case _ => Unknown
  }
}

所以我想返回类型本身,将其存储在 Map 中,以便以后可以在某处应用它:

val test = Buffer(
  ("ahaha" -> "one")
  ("ohoho" -> "two")
  ("lalala" -> "one")
)

var map = scala.collection.mutable.Map[String, Super]()

test.map {pair =>
  map(pair._1) = decide(pair._2)
}

所以以后我可以喜欢:

def act(code: String) {
  map(code) match {
    case One => doSmth[One]()
    case Two => doSmth[Two]()
    case _ => doNothing()
  }
}

我知道某些部分,例如案例类的未使用参数,在这里可能看起来很奇怪,但这就是我工作的环境中的情况,这个例子很完整,因为我不确定如果我这样做是否会有所不同拿走东西……

那么我怎样才能让decide函数返回一个类型,然后以类似于我所展示的方式使用它呢?

4

2 回答 2

4

我认为您可能想要case object One等,而不是使用 Class 或 ClassTag。然后你会得到有用的匹配支持。对于该act方法,您的案例对象可以返回 ClassTag 或类似的,或者只是让 act 将 One 与 doSmth[OneClass] 等相关联。

看来您可以将您的案例伴侣变成案例对象。那是不是很特别。

package typeswitch
import reflect.runtime.universe._

sealed trait Selection

class Super
case class One(a: Int) extends Super
case object One extends Selection
case class Two(b: Float) extends Super
case object Two extends Selection
case class Unknown() extends Super
case object Unknown extends Selection

object Test extends App {
  type What = Selection

  def decide(criterion: String): What = criterion match {
    case "one" => One
    case "two" => Two
    case _ => Unknown
  }

  val test = List(
    "ahaha" -> "one",
    "ohoho" -> "two",
    "lalala" -> "one"
  )

  val m = scala.collection.mutable.Map[String, What]()

  test map (pair => m(pair._1) = decide(pair._2))

  def act(code: String) = m(code) match {
    case One => doSmth[One]()
    // non-exhaustive
    //case Two => doSmth[Two]()
    case Unknown => doNothing()
    // handle exhaustively
    case s: Selection => doSmthNew(s)
  }
  def doSmthElse[A <: Super]()(implicit t: TypeTag[A]): A = {
    Console println s"Do st with $t"
    val claas: Class[_] = t.mirror.runtimeClass(t.tpe)
    null.asInstanceOf[A]
  }
  def doSmth[A <: Super]()(implicit t: ClassTag[A]): A = {
    Console println s"Do st with $t"
    val claas: Class[_] = t.runtimeClass
    null.asInstanceOf[A]
  }
  def doSmthNew[A >: What : ClassTag, B <: Super](what: A): B = {
    Console println s"Do st new with $what"
    null.asInstanceOf[B]
  }
  def doNothing() { }

  val res = act("lalala")
  Console println s"Got $res?"
}
于 2012-12-10T17:19:56.453 回答
3

对不起,如果这太基本了,但是:

$ scala
Welcome to Scala version 2.10.0-RC2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_06).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait Bar
defined trait Bar

scala> case class Foo(i:Int) extends Bar
defined class Foo

scala> import reflect.runtime.universe._
import reflect.runtime.universe._

scala> def f[A <: Bar : TypeTag]() = println(s" Do ${ implicitly[TypeTag[A]] }") 
f: [A <: Bar]()(implicit evidence$1: reflect.runtime.universe.TypeTag[A])Unit

scala> f[Foo]
 Do TypeTag[Foo]
于 2012-12-10T18:15:16.090 回答