How to provide default value for implicit paramete

2020-05-20 08:44发布

问题:

I'm trying to define a class with some methods taking an implicit parameter :

object Greetings {
  def say(name: String)(implicit greetings: String): String = greetings + " " +name 
}

I use this class from another class

implicit val greetings = "hello"                //> greetings  : java.lang.String = hello
Greetings.say("loic")                           //> res0: String = hello loic
Greetings.say("loic")("hi")                     //> res1: String = hi loic

My problem is that it works only if I define the implicit val outside my Greetings object. I would like to be able to provide methods with implicit parameters, with a default value inside my class, to make easier the use of my API (like Scala collection API).

So I would like to do this, but it's not working (implicit value not found) :

object Greetings {
  implicit val greetings = "hello"    
  def say(name: String)(implicit greetings: String): String = greetings + " " +name 
}

and then

Greetings.say("loic")                         
Greetings.say("loic")("hi") 

I know I can define a default value with (implicit greetings: String = "hello") but I would like to do it at class level, to avoid repeating if there are many methods.

I guess I'm missing something because I saw that CanBuildFrom is defined inside the List class, for example.

回答1:

It is a bad idea to use such a general type as String in an implicit. The main reason is that implicit lookup is solely base on the type, so what if someone else defines another implicit value of type String? You might end up with a conflict. So you should define your own specific type for your own purpose (a simple wrapper around String).

Another reason is that when looking for implicit values, the compiler will look (among other places) into the companion object (if any) of the implicit value type. You can easily see how useful it is, as the companion object is the natural place to put a default implicit value (as in your case). But if the implicit value is of a type that you don't own (such as String) you just cannot write a companion object for it, while with your own wrapper type there is no problem.

OK, enough verbiage, here is how you can do it:

case class Greetings( value: String ) {
  override def toString = value
}
object Greetings {
  // this implicit is just so that we don't have to manually wrap 
  // the string when explicitly passing a Greetings instance
  implicit def stringToGreetings( value: String ) = Greetings( value ) 

  // default implicit Greetings value
  implicit val greetings: Greetings ="hello"

  def say(name: String)(implicit greetings: Greetings): String = greetings + " " +name 
}
Greetings.say("loic")                         
Greetings.say("loic")("hi") 


回答2:

I've found a workaround:

class Greetings(implicit val greetings: String = "hello") {
    def say(name: String): String = greetings + " " + name 
}

Like this I can have a default value and override it if I want:

new Greetings().say("loic")                     //> res0: String = hello loic

implicit val greetings = "hi"                   //> greetings  : java.lang.String = hi
new Greetings().say("loic")                     //> res1: String = hi loic

new Greetings()("coucou").say("loic")           //> res2: String = coucou loic

Note: new Greetings()("coucou") is working, not new Greetings("coucou") , because of a syntax strangeness explained here.



标签: scala