David Pollak | 9 Feb 17:28
Picon
Gravatar

Re: Very weird error



On Mon, Feb 8, 2010 at 8:41 PM, Windemuth Andreas <windemut <at> yahoo.com> wrote:

Could it be because MessageWithAction is not covariant in MsgType?

Yep... PaulP showed me the errors of my ways at the Scala BASE meeting last night.  Thanks all!
 

Andreas


From: David Pollak <feeder.of.the.bears <at> gmail.com>
To: Scala list <scala <at> listes.epfl.ch>
Sent: Mon, February 8, 2010 7:44:47 PM
Subject: [scala] Very weird error

Folks,

I'm working on some Goat Rodeo code and I'm running into a huge problem with compound types.

I'm got a case class that holds a message and a function to execute on response from the message:

MessageWithAction[ActionType <: QBase,
                                   MsgType <: QBase with
                   MsgWithResponse[ActionType]](msg: MsgType,
 timeout: Long,
 action: Option[ActionType] => Unit)

I've got a worker that takes a MessageWithAction as a parameter in one its methods:

trait Worker[IdType <: WorkerId, MsgType] {
  /**
   * The unique ID of the Work
   */
  def id: IdType

  /**
   * Send a message to the worker
   */
  def !(msg: MsgType): Unit

  /**
   * Send a message to along with a callback
   */
  def !![T <: QBase](msgWithAction: MessageWithAction[T, MsgType with MsgWithResponse[T]]): Unit
}

And I've got a message:

case class MooFu(str: String) extends SimpleMsg with MsgWithResponse[QLong]

Now, when I try to send that message:

 val q = MessageWithAction(MooFu("Hello"), 400L,  (a: Option[QLong]) => println(a))

  n !! q

The compiler complains:
InferThis.scala:73: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[?,SimpleMsg with MsgWithResponse[?]]
  n !! q
       ^

I try to be nice and explicitly tell the compiler that the type parameter for !! is QLong, but I still get:

InferThis.scala:76: error: type mismatch;
 found   : MessageWithAction[QLong,MooFu]
 required: MessageWithAction[QLong,SimpleMsg with MsgWithResponse[QLong]]
  n.!![QLong](q)

I'm enclosing a complete example.

So, why doesn't the compiler recognize MooFu as a SimpleMsg with MsgWithResponse[QLong]?  How can I help things along?

Thanks,

David



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

Gmane