Nitish Upreti
Nitish Upreti

Reputation: 6550

How to take multiple functions as variable arguments in Scala ?

In order to take multiple variables as arguments for data types in Scala we can write something as :

def test(args: String*)

How can we take multiple functions as arguments in the same spirit? (The Syntax?) I could not find anything after much googling.

Upvotes: 2

Views: 212

Answers (1)

Travis Brown
Travis Brown

Reputation: 139028

Functions in Scala are values with types like any other values, and A => B (or, equivalently, Function1[A, B]) is the type of a function from A to B. So if for example you want to take a variable number of functions from strings to strings, you could write the following:

def test(funcs: (String => String)*) = ???

You could actually skip the internal parentheses and write funcs: String => String*, but I personally find the precedence there a bit unclear.

Upvotes: 8

Related Questions