This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Very weird error

2 replies
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
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
Andreas W
Joined: 2009-01-10,
User offline. Last seen 1 year 14 weeks ago.
Re: Very weird error

Could it be because MessageWithAction is not covariant in MsgType?

Andreas


From: David Pollak <feeder.of.the.bears@gmail.com>
To: Scala list <scala@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
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Very weird error


On Mon, Feb 8, 2010 at 8:41 PM, Windemuth Andreas <windemut@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@gmail.com>
To: Scala list <scala@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

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland