Reputation: 585
I have some trouble understanding the following method signature in Array.scala:
def fill[T: ClassTag](n1: Int, n2: Int)(elem: => T): Array[Array[T]]
I have read up on the concept of a polymorphic method from here: http://www.scala-lang.org/old/node/121.html
The method fill
does appear to be parameterized by type T
with value parameters. The method signature here confuses me though. It appears to be a "curried function" that takes multiple parameters but then there are two parameters n1
, and n2
in one parentheses and a function is passed in as a second parameter. That is like a higher-order function being passed into fill
. But that does not look quite right either.
If anyone can point me in the right direction with a clue I can figure out more about how the fill
method works, like the method body.
Upvotes: 0
Views: 688
Reputation: 51271
The method you've quoted here creates, and fills, a 2-dimensional array. The first 2 parameters, the Int
s, dictate the dimensions (X & Y axis) of the array.
The 3rd parameter is in a separate parameter group. That's the curried part. If it had been written fill(n1, n2, elem)
then it would not be a curried method. Or all the parameters could have been curried: fill(n1)(n2)(elem)
. In Haskell all parameters are curried.
The elem: => T
notation means that this is a "by name" parameter. It looks similar to a function definition but that's not the case. It simply means that the received parameter, elem
, is not evaluated at call time. It is evaluated when referenced, which might be never. For example:
Array.fill(x,y)(/*some-long-calculation*/)
In this case if either x
or y
are zero then the "long-calculation" is not executed.
Upvotes: 3