関数から型を返したい。例えば:
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
}
}
だから私はタイプ自体を返し、後でどこかに適用できるようにそれをマップに保存したいと思います。
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
関数が型を返すようにして、それを私が示したのと同じように使用するにはどうすればよいでしょうか。