這裏我試圖用隱式的方法調用一個函數。嘗試刪除儘可能多的鍋爐板代碼儘可能調用函數中使用:未調用Scala方法
("a" and "j", {
println("Hello");
})
但這並不援引f1
如何調用該函數f1
與呼叫
("a" and "j", {
println("Hello");
})
?
完整代碼:
object First extends App {
case class Commands(val list: List[String]) {
def and(that: String) = Commands(that :: list)
}
implicit def l(cmd: String) = Commands(cmd :: Nil)
implicit def f1(implicit d: (Commands,() => Unit)): Unit = {
val launchCommand = d._1.list.reverse.mkString("") + "::"
println(launchCommand)
println("This method is not being invoked");
d._2()
}
("a" and "j", {
println("Hello");
})
}
更新:
object First extends App {
case class Commands(val list: List[String]) {
def and(that: String) = Commands(that :: list)
}
implicit def l(cmd: String) = Commands(cmd :: Nil)
implicit def f1(implicit d: (Commands,() => Unit)): Unit = {
val launchCommand = d._1.list.reverse.mkString("") + "::"
println(launchCommand)
println("This method is not being invoked");
d._2()
}
implicit val commandAndFunc = ("a" and "j", {
println("Hello");
})
f1
}
f1
原因編譯器錯誤:
Multiple markers at this line:
◾not enough arguments for method f1: (implicit d: (First.Commands,() ⇒ Unit))Unit. Unspecified value parameter d.
◾could not find implicit value for parameter d: (First.Commands,() ⇒ Unit)
難道它現在的工作? –