I found this error message from the compiler confusing


I apologize if this is the wrong category to post in. The Scala bug reporting guide ( https://www.scala-lang.org/contribute/bug-reporting-guide.html ) suggested this category, but the category description doesn’t mention bugs, so if this isn’t appropriate, I’m sorry.

I found a confusing error in the Scala compiler. Some code that demonstrates the problem:

trait ParamTrait

class ImplementingParamTrait extends ParamTrait

trait Model[P <: ParamTrait] {
  def create(conditionalParams: P)(implicit d: Double): Int

object Obj extends Model[ImplementingParamTrait] {
  def create(conditionalParams: ImplementingParamTrait)(d: Double): Int =

When trying to compile this (through both sbt and scalac), I get the following error:

src/main/scala/ErrorMessageExample.scala:9: error: object creation impossible, since method create in trait Model of type (conditionalParams: ImplementingParamTrait)(implicit d: Double)Int is not defined
(Note that P does not match ImplementingParamTrait)
object Obj extends Model[ImplementingParamTrait] {
one error found

Now, the error in the code seems to be that there is no implicit keyword in the second set of params for the create method definition in Obj. If you change it to read

def create(conditionalParams: ImplementingParamTrait)(implicit d: Double): Int = 5

then it compiles without a problem.

However, I was curious why the compiler includes the message Note that P does not match ImplementingParamTrait in the error message. For one, it doesn’t seem to have anything to do with the actual problem, and secondly, as far as I can tell, inside Obj, P should be ImplementingParamTrait.

Can someone help me understand the error message? Or, if this is a bad error message, can someone direct me as to where to file a bug request?


Thanks for the link to the bug reporting guide. It turns out I’ve been doing it all wrong.

Your confusion was partially addressed under this issue so I added your test case and addressed it.

I’ve always wanted to use asSeenFrom, and I finally got my chance. That’s what shows your P is ImplementingParamTrait at the point of your error.

I don’t think I knew implicit mattered for purposes of overriding. I don’t see it in the spec. But it would mean, if I change from (x: Model).create(p) to Obj.create(p), I get an error, when all I did was substitute a subtype.


Cool! I guess my search wasn’t good enough to turn up that Github issue before. :disappointed:

Thanks for making a patch, though!