I am fairly new to Scala and have been trying to learn and understand implicit conversions and parameters and have encountered a scenario that I find confusing.
For context, I am using Scaldi to do dependency injection in an Akka application and would like to have multiple injectable actors inherit from an abstract class. I believe I am unable to make the abstract class a trait precisely because we need to make an implicit Injector
available via a constructor argument to take advantage of the framework.
A very contrived example that exhibits the behavior that I am seeing is as follows:
class SecretSauce {}
abstract class Base(implicit secretSauce: SecretSauce) {}
class Concrete extends Base {}
object Example extends App {
... // Setup Actor system, etc, etc
implicit val secretSauce: SecretSauce = new SecretSauce()
}
I was expecting things to work but instead I get a compilation error:
Unspecified value parameter secretSauce.
class Concrete extends Base {
^
If I add the implicit parameter to the concrete class, like such, things work:
class Concrete(implicit secretSauce: SecretSauce) extends Base {}
I think my confusion stems from how implicit parameters work - in situations like the one I'm describing, are they not inherited by child classes? Can someone ELI5 what is occurring in my example or point me to a reference that can help clear things up?
Thanks!
class Concrete(implicit secretSauce: SecretSauce) extends Base {}
From the way I've set it up in my code, the implicit value is still not in scope nor is it in a companion object, right? Is my understanding of scope here off? – Motivate