Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leaving out some implicit parameters

Is it possible to leave out some implicit parameters but not all of them? I tried with named parameters:

def foo(implicit a: Int, b: String) {
  if (a > 0) {
    println(b)
    foo(a = a-1)   // error
  }
}

Unfortunately, the compiler rejects the recursive call of foo with:

not enough arguments for method foo
Unspecified value parameter b
like image 478
fredoverflow Avatar asked Feb 15 '26 08:02

fredoverflow


1 Answers

It is not possible to leave out some implicit parameters. So, in your example

def foo(implicit a: Int, b: String): Unit = ???

It is not possible to only specify a. However, you can specify the default value of the implicit parameter, for example

def foo(implicit a: Int, b: String = "---"): Unit = ???

Where if b is not implicitly available, "---" will be used.

Remember that the implicit keyword marks the parameter list as implicit, not that one parameter as implicit.

like image 91
janm399 Avatar answered Feb 16 '26 22:02

janm399



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!