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.