Question

I'm struggling with the following problem which throws an error at compile-time " error: value dir is not a member of type parameter A". But it is!

trait Logger { def dir: String }

trait LoggerFile[A <: Logger] {
  def extractor: String => Option[A]
}

def getHistory[A: LoggerFile](): String = {
  implicitly[LoggerFile[A]].extractor("abc") match {
    case Some(a) => a.dir
    case None => ""
  }
}

I was able to overcome the problem by using this answer:

def getHistory[A <: Logger]()(implicit env: LoggerFile[A]): String = {

But I would have preferred the system to work before the transformation, i.e. with the syntactic sugar. Is there a way to specify multiple type constraints on A?

Was it helpful?

Solution

Just put all the constraints together.

After changing the type signature to

def getHistory[ A <: Logger : LoggerFile ](): String

your example compiles just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top