我想将一个闭包作为选项传递给一个方法,我正在做如下所示的事情。我收到如下所示的编译错误。是否可以将可选的闭包参数传递给函数?
def sampleMethod(a: String, b: String, optionalMethod: Option[(String, Int) => Unit]) {
// do some processing with a and b
optionalMethod match {
case Some(optionalMethod) => {
optionalMethod("a",3)
}
case _
log("no optional method passed")
}
}
// definition of optMethod in some other place
val optMethod = (c: String, d: Int) => {
// some processing with c, d and external values
}
// invoke
sampleMethod("hi", "bye", optMethod) => FAILS TO COMPILE
ERROR = type mismatch. expecting Option[(String, Int) => Unit] found (String, Int) => Unit