Reputation: 105
I am a newbie fascinated by Scala looking for a way of creating a method/function that takes a variable amount of functions as parameters.
Example with the *
notation used in regular VarArgs
def someMethod(aNumber: Int, multipleFunctions: Int => Boolean*) = {
var flag = true
for (func <- multipleFunctions; if (!func(aNumber)) flag = false
flag
}
I realize I could accept an array of functions, but if that works it just feels like there must be a way of doing it with var args.
Upvotes: 1
Views: 109
Reputation: 12804
The *
token is evaluated with precedence over =>
, so you can simply solve your problem by wrapping the Int => Boolean
type in parentheses:
def someMethod(aNumber: Int, multipleFunctions: (Int => Boolean)*)
// ^ here! ^
Upvotes: 1