Scala で、可変数の引数を取る無名関数を定義するにはどうすればよいですか?
scala> def foo = (blah:Int*) => 3
<console>:1: error: ')' expected but identifier found.
def foo = (blah:Int*) => 3
^
Scala で、可変数の引数を取る無名関数を定義するにはどうすればよいですか?
scala> def foo = (blah:Int*) => 3
<console>:1: error: ')' expected but identifier found.
def foo = (blah:Int*) => 3
^
これは不可能のようです。章 6.23匿名関数の言語仕様では、構文で型の後に an を使用できません。章 4.6関数の宣言と定義では、型の後に.*
*
ただし、できることは次のとおりです。
scala> def foo(ss: String*) = println(ss.length)
foo: (ss: String*)Unit
scala> val bar = foo _
bar: (String*) => Unit = <function1>
scala> bar("a", "b", "c")
3
scala> bar()
0