let's say that I would like to add some method to String class. But concrete implicit class that should be applied is known during runtime (strategy pattern). Lets say we have
trait StringExtensions {
def doSth(str: String): String
}
class Strategy1 extends StringExtensions {
override def doSth(str: String): String = "a"
}
class Strategy2 extends StringExtensions {
override def doSth(str: String): String = "b"
}
now my client code looks like:
def someMethod(strategy: StringExtensions) : String{
val name = "Pawel"
return strategy.doSth(name)
}
...
String ret = someMethod(new Strategy1())
but I would like to have code like:
def someMethod(strategy: StringExtensions) : String{
val name = "Pawel"
return name.doSth() // Here is the tricky line
}
...
String ret = someMethod(new Strategy1())
I played a bit with implicits but when comes to this use case with inheritance I cannot find proper solution, any help?