2
votes

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?

1
It compiles for me: scastie.scala-lang.org/BalmungSan/yRtIa6cMS2GktJzTi1q0Bg - Are you sure the error is real? maybe is just IntellIJ. Have you checked using your build tool? - If it is real, then are you sure you have all you need to reproduce it in that example? Maybe specify the Scala version.Luis Miguel Mejía Suárez
Amended the question to include Scala version, thanks for the reminder! I encountered the error when using sbt and reproduced it in the spark-shell.largecats
I tried the 2.11.12 scala version in Scastie and was able to reproduce the error. Seems to be a version issue. Thanks a lot for sharing this!largecats

1 Answers

1
votes

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