raisercostin
raisercostin

Reputation: 9189

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.

Upvotes: 2

Views: 448

Answers (3)

raisercostin
raisercostin

Reputation: 9189

An workaround a value with apply as suggested by Rob Norris

object Main {
  def main(args: Array[String]) = {
    {
      //workaround use Seq as requested
      def collectBigger(median: Int)(values: Int*) = values.filter { _ > median }
      val passedRanks = collectBigger(5)_
      println(passedRanks(Seq(5, 9, 5, 2, 1, 3)))
      println(passedRanks(5, 9, 5, 2, 1, 3))//compile error: too many arguments for method apply: (v1: Seq[Int])Seq[Int] in trait Function1
    }

    {
      //
      def collectBigger(median: Int) = new { def apply(values: Int*) = values.filter { _ > median } }
      val passedRanks = collectBigger(5)
      import scala.language.reflectiveCalls
      println(passedRanks(Seq(5, 9, 5, 2, 1, 3)))//compile error (as expected): type mismatch; found : Seq[Int] required: Int

      //this now works
      println(passedRanks(5, 9, 5, 2, 1, 3))
    }
  }
}

Upvotes: 0

som-snytt
som-snytt

Reputation: 39577

You used to be able to:

$ scala210 -Yeta-expand-keeps-star
Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.7.0_95).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def f(vs: Int*) = vs.sum
f: (vs: Int*)Int

scala> f(1,2,3)
res0: Int = 6

scala> val g = f _
g: Int* => Int = <function1>

scala> g(1,2,3)
res1: Int = 6

But no longer.

https://issues.scala-lang.org/browse/SI-6816

Upvotes: 0

Dima
Dima

Reputation: 40500

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.

Upvotes: 10

Related Questions