Here I'm attempting to invoke a function with the use of implicit's. To try to remove as much boiler plate code as possible invoking function using :
("a" and "j", {
println("Hello");
})
But this does not invoke f1
how to invoke the function f1
with the call
("a" and "j", {
println("Hello");
})
?
Complete code :
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");
})
}
Update :
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
causes compiler error :
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)
implicit val commandAndFunc: (Commands, () => Unit) = ("a" and "j", { () =>
println("Hello")
})
f1
this would invoke f1 with commandAndFunc.
You just define a tuple of a command and a function. Why should it call f1?
How should the compiler/the program know, if it is a call to f1 or just a declaration of a tuple of type Tuple2[Command,() => Unit]
?
With implicit you can hand over a parameters without writing them explicit or you can convert an object implicit. You can not make the compiler know magically what you want to call.