we know scala not support more 22 params, if write this
def echo(args: string*) = (arg <- args) println(arg)
we can use more 22 params call function this.
echo("1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1")
but think array. so, can , tried this
val = array[string]("1","2","3");echo(a)
this code must wrong, here's first question, why happening? and, if try write this
echo(a : _*)
it's right,the second question is, sign means '_*'? can't use code in other ways in for(). so, echo(a : _ *) right code?
the echo function defined take variable number of string arguments. syntactic sugar; compiler insert necessary instructions wrap arguments in array , pass array. function receive single argument @ runtime.
the reason can't pass array directly there no additional compiler logic automagically figure out string arguments wrapped. function declaration indicates 0 or more strings expected, parameter array, , compiler error results.
the : _* notation additional syntactic sugar account problem; using syntax indicate compiler intentionally passing array instead of variable number of string parameters.