largecats
largecats

Reputation: 175

Scala Could not find implicit value supplied by mixin

Using Scala version 2.11.12.

I can't seem to make the implicit config supplied by the mixin Foundation trait visible to the Path class' constructor.

case class Config() {
  val content = "hello"
}

trait Foundation {
  implicit val config: Config = Config()
}

trait PathBase {
  def somePath: String = "/user/xxx"
}

class Path(implicit val config: Config) extends PathBase {
  def someOtherPath: String = "/user/yyy"
}

trait NavigatorBase {
  protected implicit val hdfs: PathBase
}

trait Navigator extends NavigatorBase with Foundation {
//  private implicit val config1: Config = config
  protected implicit val hdfs = new Path
}

The above code raises the following error:

:39: error: could not find implicit value for parameter config: Config protected implicit val hdfs = new Path

However, if I define a new implicit val using the config supplied by Foundation before calling Path's constructor (see the commented line in the code), the code works. If I write trait Navigator extends Foundation without mixing in NavigatorBase, the code also works.

Is there a way to make the implicit config visible to Path's constructor without having to define a new implicit variable or remove the extension of NavigatorBase?

Upvotes: 2

Views: 88

Answers (1)

largecats
largecats

Reputation: 175

Solution: As pointed out by @Luis Miguel Mejía Suárez in the comment, use the latest version of Scala.

Scala 2.11.12 raises the error: https://scastie.scala-lang.org/yZU850eUQvKSt6LJ0GIeVA

Scala 2.13.4 works fine: https://scastie.scala-lang.org/8FYzJdimSOanOLow25rN3g

Upvotes: 1

Related Questions