I wrote a class implementing the command design pattern:
class MyCommand[-T, +R](val name: String, val execute: T => R)
, prepared two command and stored it in a 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)
Then I have two data to be executed:
val data = Array("StRiNG", new Date())
The problem for me is that I don't know how to determine which datum is applicable to the command:
data.foreach {
d => commands.foreach {
c =>
// println(c.execute(d)) if d is applicable to c.execute().
}
}
what I tried is pattern matching with type specification, but it yields syntax error:
c.execute match {
case m: (d.getClass => Any) => println(c.execute(d))
}
Help me :(