使用泛型参数运行函数而不使用asInstanceOf

时间:2017-02-05 13:09:35

标签: scala generics

这是我的示例scala代码:

object App {
  abstract class BaseAction
  type ApiAction[T <: BaseAction] = (T) => Unit

  case class FirstAction(name: String) extends BaseAction
  case class SecondAction(surname: String) extends BaseAction

  def action1[Z <: BaseAction] = {  
    (a: Z) => { // Here i'would like to have a: FirstAction
      val z = a.asInstanceOf[FirstAction]
      println("Running action: " + z.name )
    }
   }

 def action2[Z <: BaseAction] = {
  (a: Z) => { // Here i'would like to have a: SecondAction
  val z = a.asInstanceOf[SecondAction]
  println("Running action " + z.surname )
 }
}

  def myActions[T <: BaseAction] = Map[String, ApiAction[T]]("a1" -> action1[T], "a2" -> action2[T])

  myActions("a1")(FirstAction("Action 1"))
  myActions("a2")(SecondAction("Action 2"))
}

我有很少的动作功能,它们做了不同的事情。 每个动作函数都接收一个参数:action class,其中所有动作类都继承自BaseAction抽象类。

功能myActions是actionName到action函数的映射。

我的代码正在运行,但我认为使用asInstanceOf不是一个好习惯,我想知道如何只使用泛型类型编写此代码,而不使用asInstanceOf。

2 个答案:

答案 0 :(得分:0)

问题是你做了很多事情&#34;你不应该做这些事情&#34;。

我会尽力给你更好的&#34; (根据大多数斯卡拉人)写同样的事情的方式。这些更改包括使用来自类型边界的信息以及自定义类型ApiAction来编写更可预测和有组织的代码。

首先,你有以下抽象,

  abstract class BaseAction

  type ApiAction[T <: BaseAction] = (T) => Unit

  case class FirstAction(name: String) extends BaseAction
  case class SecondAction(surname: String) extends BaseAction

现在您可以使用这些抽象来编写Actions对象,

object MyActions {

  val action1: ApiAction[FirstAction] = {
    case FirstAction(name) => println("Running action :: " + name)
  }

  val action1Other: ApiAction[FirstAction] = (fa: FirstAction) => {
    println("Running action :: " + fa.name)
  }

  val action2: ApiAction[SecondAction] = {
    case SecondAction(surname) => println("Running action :: " + surname)
  }

  val action2Other: ApiAction[SecondAction] = (sa: SecondAction) => {
    println("Running action :: " + sa.surname)
  }

  // but lets say you wanted a generic ApiAction
  val actionGeneric: ApiAction[BaseAction] = {
    case FirstAction(name) => println("Running action :: " + name)
    case SecondAction(surname) => println("Running action :: " + surname)
  }

}

现在你可以使用这些&#34;动作&#34;如你所愿,在你的应用程序中,

object MyApp extends App {

  MyActions.action1(FirstAction("Action 1"))

  MyActions.action1Other(FirstAction("Action 1 Other"))

  MyActions.actionGeneric(FirstAction("Action 1 Generic"))

  MyActions.action2(SecondAction("Action 2"))

  MyActions.action2Other(SecondAction("Action 2 Other"))

  MyActions.actionGeneric(SecondAction("Action 2 Generic"))
}

答案 1 :(得分:0)

您可以使用惯用的Scala模式匹配而不是asInstanceOf

val z = a match {
  case FirstAction(name) => println("Running action " + name)
  case _ => println("Error")
}

请注意,模式匹配仍然使用isInstanceof + asInstanceOf,但与直接调用asInstanceOf不同,它被视为良好做法。

顺便说一下,组织你的代码可能是明智的,这样你只需匹配一次,而不是有两个单独的“第一个动作或错误”和“第二个动作或错误”块:

def action[Z <: BaseAction] = {
  (a: Z) => a match {
    case FirstAction(name) => println("Running action " + name)
    case SecondAction(surname) => println("Running action " + surname)
    case _ => println("Error")
  }
}

def myActions[T <: BaseAction] = Map[String, ApiAction[T]]("a1" -> action[T], "a2" -> action[T])

myActions("a1")(FirstAction("Action 1"))
myActions("a2")(SecondAction("Action 2"))

// output:
// Running action Action 1
// Running action Action 2