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

Answers


Just put all the constraints together.

After changing the type signature to

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

your example compiles just fine.


Need Your Help

Setting width as a percentage using jQuery

jquery width

How can I set the width of a div as a percentage using jQuery?

Are Lisp source code files themselves lists?

emacs clojure lisp scheme common-lisp

No matter the Lisp dialect, it looks like every source code file containing Lisp functions isn't itself a list (the first time I was "surprised" by this was when working on my Emacs .el files).