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?
Try
println(f(y = 2, a = implicitly))
Once you start specifying parameters, you can't go back. It's either the whole list is implicit or none of it is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With