具有部分应用函数的变量参数

时间:2016-05-30 13:03:00

标签: scala variadic-functions partialfunction

我在以下代码中遇到了编译问题。

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))
    }
}

该示例的灵感来自com.agical.gsl,它是swt的scala适配器。我假设它在scala 2.8之前使用了scala功能。

错误是too many arguments for method apply: (v1: Seq[Int])Seq[Int] in trait Function1,并且关于如何将变量参数传递给部分应用的函数。

感谢您提供的任何提示。

3 个答案:

答案 0 :(得分:9)

简单地说,你可以在scala中使用varargs 方法,但不能使用varags 函数。为什么?好吧,所有函数都有FunctionN[T1..TN,R]类型。在您的情况下,它是Function1[Seq[Int], Seq[Int]]

" varargs函数"根本就没有类型,所以无论何时将方法转换为函数,都必须将其置为Seq..符号。

答案 1 :(得分:0)

您曾经能够:

$ 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

但不再。

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

答案 2 :(得分:0)

使用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))
    }
  }
}