2

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.

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

For completeness: def test(args: Function1[String, String]*)
Always keep in mind the option to declare a type alias. In this case, e.g., type Str2Str = String => String then def test(funcs: Str2Str*).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.