scala - 在 Scala 中在运行时对协变和逆变类进行有效的类型转换

标签 scala types covariance contravariance

我写了一个实现 command design pattern 的类:

class MyCommand[-T, +R](val name: String, val execute: T => R)

,准备了两个命令并将其存储在一个 MutableList 中:
val commands = new mutable.MutableList[MyCommand[Nothing, Any]]
commands += new MyCommand[String, String]("lower", s => s.toLowerCase())
commands += new MyCommand[Date, Long]("time", d => d.getTime)

然后我有两个数据要执行:
val data = Array("StRiNG", new Date())

我的问题是我不知道如何确定哪个数据适用于命令:
data.foreach {
  d => commands.foreach {
    c =>
    // println(c.execute(d)) if d is applicable to c.execute().
  }
}

我尝试的是与类型规范匹配的模式,但它会产生语法错误:
c.execute match {
  case m: (d.getClass => Any) => println(c.execute(d))
}

帮我 :(

最佳答案

我很确定有更好的方法来解决这个问题,但这可能对你有用。我用 Scala 2.9.2 对其进行了测试。
MyCommand需要一个 Manifest作为额外的隐式参数,它使我们可以访问表示 execute 的 from 类型的类。运行时函数:

class MyCommand[-T: Manifest, +R](val name: String, val execute: T => R) {
  val fromClass = manifest[T].erasure
}

命令列表基本上与您的原始帖子中的一样,数据列表也是如此:
val commands = List(
  new MyCommand[String, String]("lower", _.toLowerCase()),
  new MyCommand[Date, Long]("time", _.getTime)
)

val data = List("StRiNG", new Date())

将数据与命令匹配依赖于所涉及类型作为类的运行时表示,以及相当丑陋的强制转换。强制转换特别难看,因为它没有给出精确的返回类型,即,如果您需要知道命令返回值的精确返回类型,则需要额外的匹配或强制转换。
data foreach { d =>
  commands foreach { c =>
    println("data: %s (%s), command takes: %s"
           .format(d, d.getClass.getSimpleName, c.fromClass.getSimpleName))

    if (d.getClass.isAssignableFrom(c.fromClass)) {
      println("    cmd(data) = " + c.execute.asInstanceOf[Any => Any](d))
    }
  }
}

输出是:
data: StRiNG (String), command takes: String
    cmd(data) = string
data: StRiNG (String), command takes: Date
data: Sun Aug 05 14:46:17 CEST 2012 (Date), command takes: String
data: Sun Aug 05 14:46:17 CEST 2012 (Date), command takes: Date
    cmd(data) = 1344170777681

关于scala - 在 Scala 中在运行时对协变和逆变类进行有效的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815908/

相关文章:

scala - Spark数据集联合重置类变量

scala - Spark 中的性能调整

scala - 无所苦

TypeScript:强制字符串文字类型只有一个值

scala - Scala中的协方差逆变错误

java - Class#getInterfaces() 和 Class#getGenericInterfaces() 返回不同长度的数组

python - 如何从 Spark SQL 查询 [PySpark] 获取表名?

c# - 将通用类委托(delegate)给特定类

c - void * 函数的输入在函数结束时丢失地址

c++ - 派生类未被识别为协变