Question

Consider the following:

def f(implicit a: String, y: Int = 0) = a + ": " + y
implicit val s = "size"
println(f(y = 2))

The last expression causes the following error:

not enough arguments for method f: (implicit a: String, implicit y:
Int)java.lang.String. Unspecified value parameter a.

However, if you provide a default value to the implicit parameter a, there is no issue:

def f(implicit a: String = "haha!", y: Int = 0) = a + ": " + y
implicit val s = "size"
println(f(y = 2))

But the last line prints

haha!: 2

while I would have expected

size: 2

So the implicit value 's' is not picked up. If you instead don't provide any parameters to f and just call

println(f)

then the implicit value is picked up and you get

size: 0

Can someone shed some light on what's going on here?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top