4

Is it possible to pass function arguments from a iterable in Scala?

val arguments= List(1,2)
def mysum(a:Int,b:Int)={a+b}

How would one call mysum using the List contents as arguments?

scala_newbie
  • 3,315
  • 2
  • 11
  • 13

1 Answers1

3

For it to work on lists you'll need to accomodate the mysum function to "varargs":

scala> def mysum ( args : Int* ) = args.sum
mysum: (args: Int*)Int

scala> val arguments = List(1,2)
arguments: List[Int] = List(1, 2)

scala> mysum(arguments: _*)
res0: Int = 3
Nikita Volkov
  • 42,103
  • 11
  • 91
  • 164