Search code examples
scalavariadic-functionspartialfunction

Variable arguments with partial applied functions


I have a compilation problem on the following code.

object Main {
    def main(args:Array[String]) = {
      def collectBigger(median:Int)(values:Int*) = values.filter { _ > median }
      val passedRanks = collectBigger(5)_
      //this compiles
      println(passedRanks(Seq(5,9,5,2,1,3)))
      //this doesn't
      println(passedRanks(5,9,5,2,1,3))
    }
}

The sample is inspired from com.agical.gsl which is a scala adapter to swt. I assume it used scala features before scala 2.8.

The error is too many arguments for method apply: (v1: Seq[Int])Seq[Int] in trait Function1 and is connected on how the variable arguments are passed to a partially applied function.

Thanks for any hints that you could give.


Solution

  • To put it simply, you can have a varargs method in scala, but not a varags function. Why? Well, all functions have a FunctionN[T1..TN,R] type. In your case, it is Function1[Seq[Int], Seq[Int]].

    There is simply no type for a "varargs function", so whenever you convert a method to a function, it must be desugared into the Seq.. notation.