1

I get scalac exception on attempt to use upper bound and context bound at the same time. Is it even allowed ? I'm on Scala 2.11.8

Consider this

import spray.json._

abstract class CrossRefMessage

case class CrossRefResponse[T <: CrossRefMessage](status: String, `message-type`: String, `message-version`: String, message: T)

implicit def CrossRefResponseFormat[T <: CrossRefMessage](implicit reader: JsonFormat[T]) = jsonFormat4(CrossRefResponse.apply[T])

on compilation I get

Error:scalac: Error: type mismatch;
 found   : String
 required: co.zzzz.server.JournalsManager.CrossRefMessage
scala.reflect.internal.Types$TypeError: type mismatch;
 found   : String
 required: co.zzzz.server.JournalsManager.CrossRefMessage
    at scala.tools.nsc.typechecker.Contexts$ThrowingReporter.handleError(Contexts.scala:1402)
    at scala.tools.nsc.typechecker.Contexts$ContextReporter.issue(Contexts.scala:1254)
    at scala.tools.nsc.typechecker.Contexts$Context.issue(Contexts.scala:573)
    at scala.tools.nsc.typechecker.ContextErrors$ErrorUtils$.issueTypeError(ContextErrors.scala:106)

What am I missing ?

UPDATE: It looks like magic to me but error goes away if I change position of field message: T. It compiles if it's on 1st or 2nd position. Can anyone explain why ?

expert
  • 29,290
  • 30
  • 110
  • 214

0 Answers0