How to pass by-name repeated parameters in Scala?
The following code fails to work:
scala> def foo(s: (=> String)*) = {
<console>:1: error: no by-name parameter type allowed here
def foo(s: (=> String)*) = {
^
Is there any other way I could pass a variable number of by name parameters to the method?
This isn't very pretty but it allows you to pass byname parameters varargs style
def printAndReturn(s: String) = {
println(s)
s
}
def foo(s: (Unit => String)*) {
println("\nIn foo")
s foreach {_()} // Or whatever you want ...
}
foo()
foo((Unit) => printAndReturn("f1"),
(Unit) => printAndReturn("f2"))
This produces
In foo
In foo f1 f2