4

我写了一个实现命令设计模式的类:

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))
}

帮我 :(

4

1 回答 1

2

我很确定有更好的方法来解决这个问题,但这可能对你有用。我用 Scala 2.9.2 对其进行了测试。

MyCommand将 aManifest作为附加的隐式参数,它使我们能够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
于 2012-08-05T12:54:50.217 回答