未调用 Scala 方法

Scala method not being invoked

我在这里尝试使用隐式调用函数。尝试使用调用函数删除尽可能多的样板代码:

("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)
  implicit val commandAndFunc: (Commands, () => Unit) = ("a" and "j", { () =>
    println("Hello")
  })

f1

这将使用 commandAndFunc 调用 f1。
您只需定义命令和函数的元组。为什么要调用 f1?
compiler/the 程序如何知道它是对 f1 的调用还是只是对类型 Tuple2[Command,() => Unit] 的元组的声明?
使用隐式,您可以在不显式编写参数的情况下移交参数,或者您可以隐式转换对象。你不能让编译器神奇地知道你想调用什么。

另一个选项:

 case class Commands(val list: List[String]) {
    def and(that: String) = Commands(that :: list)
  }

  implicit def l(cmd: String) = Commands(cmd :: Nil)

  implicit class f1(d: (Commands, () => Unit)) {

    def exec(): Unit = {
      val launchCommand = d._1.list.reverse.mkString("") + "::"
      println(launchCommand)

      println("This method is not being invoked");
      d._2()
    }
  }

  val b = ("a" and "j", () => println("Hello"))

  b.exec()