I have typeclass:
trait ProcessorTo[T]{ def process(s: String): T } and its implementation
class DefaultProcessor extends ProcessorTo[String]{ def process(s: String): String = s } trait DefaultProcessorSupport{ implicit val p: Processor[String] = new DefaultProcessor } To make it available for using I created
object ApplicationContext extends DefaultProcessorSupport with //Some other typeclasses But now I have to add a processor which performs some DataBase - read. The DB URL etc are placed in condifguration file that is available only a runtime. For now I did the following.
class DbProcessor extends ProcessorTo[Int]{ private var config: Config = _ def start(config: Config) = //set the configuration, open connections etc //Other implementation } object ApplicationContext{ implicit val p: ProcessorTo[Int] = new DbProcessor def configure(config: Config) = p.asInstanceOf[DbProcessor].start(config) } It works for me, but I'm not sure about this technique. Looks strange for me a little bit. Is it a bad practice? If so, what would be a good solution?
implicit ev: Processor[T]? What is thisApplicationContextobject?ApplicationContextobject contains typeclasses needed to be imported.implict ev: Processor[T], I need to configure theDbProcessoranyway.