The error message is pretty explicit: sampleMethod expects Option , but you pass a direct value to the function (not wrapped in Some ).
The easiest way to fix this is to wrap optMethod in Some :
sampleMethod("hi", "bye", Some(optMethod))
But if you just want to make sampleMethod("hi", "bye", optMethod) , you can add overloaded sampleMethod definitions:
object Test { 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") } } def sampleMethod(a: String, b: String) { sampleMethod(a, b, None) } def sampleMethod(a: String, b: String, optionalMethod: (String, Int) => Unit) { sampleMethod(a, b, Some(optionalMethod)) } } val optMethod = (c: String, d: Int) => { // some processing with c, d and external values } // invoke Test.sampleMethod("hi", "bye", optMethod) // Now Compiles fine Test.sampleMethod("hi", "bye") // This too
source share