Reputation: 600
I would like to use length of provided array inside parameters list. I tried
def find(xs: Array[Double], m:Int = xs.length*2) = ???
,but xs is not accessible for m parameter. Is it possible to do that? How?
Upvotes: 1
Views: 749
Reputation: 27356
In this case an ugly alternative is to use an invalid length as the default:
def find(xs: Array[Double], _m: Int = -1) = {
val m = if (_m >= 0) _m else xs.length
Upvotes: 0
Reputation: 44918
When defining default values of arguments, you can refer only to variables in the previous argument lists. Therefore, you need two argument lists:
def find(xs: Array[Double])(m: Int = xs.size * 2) = ???
Then you can call it as follows:
find(Array(1, 2, 3))(6)
and
find(Array(1, 2, 3))()
Upvotes: 2