简体   繁体   中英

Scala Implicit parameter bound

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, ie with the syntactic sugar. Is there a way to specify multiple type constraints on A?

Just put all the constraints together.

After changing the type signature to

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

your example compiles just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM