score:8

Accepted answer

in scala functions, whenever you omit a parameter (for a default value), all the following parameters (if provided) are required to be provided with names.

so, if you gave a function like following,

def abc(i1: int, i2: int = 10, i3: int = 20) = i1 + i2 + i3

you can use it in following ways,

abc(1)

abc(1, 2)

abc(1, 2, 3)

but if you want to use default value of i2 and provide a value for i3 then,

abc(1, i3 = 10)

so, in your case, you can use it like following,

val task = task(n = 100)

score:1

the important point here is: if you have a class/method definition that take n parameters but you need pass to only 1 to n-1 arguments; you use the name of the arguments with = sign and then the value of the argument you want to pass on, so in your case:

val task = task(n=2)

score:3

if you have task class defined like this case class task(uuid: string = java.util.uuid.randomuuid().tostring, n: int), you may create new instance only with n argument in such way:

task(n = 1)

Related Query

More Query from same tag