As we know that we can define a multi-parameter function in scala, like this:
def func(s: String*) = println(s)
Actually, that's not a function, that's a method. The two are fundamentally different in Scala.
My question is how to rewrite the above in function literal style. Or is it impossible to do that ?
Note: (s: String*) => println(s)
is not correct.
You cannot define a varargs argument in a function literal. There's a bug report about this in the Scala bug tracker and a comment by Martin Odersky himself that basically says that this would be much too complicated.
There are several ways to cheat, however.
If you use type inference for the function arguments, i.e. if you use the function literal in a context where the argument is statically known to be a varargs argument, then everything works fine:
val func: (String*) => Unit = s => println(s)
Alternatively, you can define a method and then convert it to a partially applied function via η-expansion:
def meth(s: String*) = println(s)
val func = meth _