4

I have the following case class:

case class Alert[T <: Transport](destination: Destination[T], message: Message[T])

In Scala 2.9.2, the following method signature has compiled fine:

def send(notification: Alert[_]) {
  notification match {
    ...
  }
}

Now in Scala 2.10.1 it fails to compile with the following error:

type arguments [_$1] do not conform to class Alert's type parameter bounds [T <: code.notifications.Transport]

Why is this? How can I fix the error? Simply giving the same type bounds to send results in a lot more compile errors...

Update: Looking at SIP-18, I don't think the reason is that I don't have existential types enabled, as SIP-18 says it's only needed for non-wildcard types, which is exactly what I do have here.

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
pr1001
  • 21,727
  • 17
  • 79
  • 125

1 Answers1

2

There error seems to be saying that the existential type "_" is not constrained to be a subtype of Transport. This may be the preferred solution,

trait Transport
trait Destination[T]
trait Message[T]
case class Alert[T <: Transport](destination: Destination[T], message: Message[T])

def send[T <: Transport](notification: Alert[T]) {
  notification match {
    case _ => ()
  }
}

This also seems to work,

def send(notification: Alert[_ <: Transport])

but I think it's preferable not to use existential types.

Kipton Barros
  • 21,002
  • 4
  • 67
  • 80
  • I had already tried the first approach and it just caused more compiler errors, but the second one works for me. – pr1001 Apr 02 '13 at 16:33
  • As for not using existential types, what would you recommend instance? – pr1001 Apr 02 '13 at 16:35
  • I don't get any compilation errors with the first approach--maybe you need to include more code to make them appear? – Kipton Barros Apr 02 '13 at 16:36
  • Sorry, of course. I then get: `type mismatch; [error] found : Seq[code.notifications.Alert[Product with Serializable with code.notifications.Transport]] [error] required: Seq[code.notifications.Alert[T]] [error] def toAlerts[T <: Transport]: Seq[Alert[T]] = subject.toSeq.flatMap(subject => {` – pr1001 Apr 02 '13 at 16:40
  • But yes, basically I try to follow the chain back and I keep getting these errors about `Product with Serializable with code.notifications.Transport` not matching `T`, where `T <: Transport`. – pr1001 Apr 02 '13 at 16:46
  • I'd like to help, but I can't reproduce your errors with the code you have posted. If you can post more code, then I could try to debug the errors. – Kipton Barros Apr 03 '13 at 01:54
  • Thanks, Kipton, I think it's easiest to just go with your solution for now. – pr1001 Apr 03 '13 at 08:04