1

I have the following code but it doesn't compile. I think the problem is that I need to tell it that T has to be a subclass of Event?

The type of scala.swing.Reactions.Reaction is PartialFunction[Event, Unit]

The error is: type mismatch; found : PartialFunction[T,Unit] required: scala.swing.Reactions.Reaction

import scala.swing.event.MousePressed
import scala.swing.Component
import scala.swing.Reactions

import reactive.EventSource
import reactive.EventStream

class TypeIssue {
  type PartialAdapter[T] = (T => Unit) => PartialFunction[T, Unit]

  def adMousePressed(c: Component)(f: MousePressed => Unit): PartialFunction[MousePressed, Unit] = {
    case MousePressed(`c`, point, mods, clicks, triggersPopup) => f
  }

  def eventToStream[T](r: Reactions, ad: PartialAdapter[T]): EventStream[T] = {
    val v = new EventSource[T] {}
    r += ad(e => v.fire(e)) // error on this line
    v
  }
}
3
  • 1
    For all those, who try to reproduce the error, it is very helpful if you include the needed import-statements, like import scala.swing.event._ It might be 5 more lines, but instead of 100 users, trying to find out, which import is necessary, ... - it's just such a waste of time. Commented Aug 3, 2011 at 19:11
  • Sorry about that. I updated question with imports and class declaration so it can be reproduced easily. Commented Aug 3, 2011 at 19:19
  • That's a fine question now. :) Commented Aug 3, 2011 at 19:40

1 Answer 1

2

ad returns a PartialFunction[MousePressed, Unit]. Reactoins+= expects a Reaction which is PartialFunction[Event, Unit]. PartialFunction is contravariant in its first type argument, so PartialFunction[MousePressed, Unit] is not considered as a PartialFunction[Event, Unit]

Just make adreturn type a Reaction. Here is the code (without the reactive types)

import scala.swing.event.MousePressed
import scala.swing.Component
import scala.swing.Reactions


class TypeIssue {
  type PartialAdapter[T] = (T => Unit) => Reactions.Reaction

  def ad(c: Component)(f: MousePressed => Unit): Reactions.Reaction = {
    case MousePressed(`c`, point, mods, clicks, triggersPopup) => f
  }

  def eventToStream[T](r: Reactions, ad: PartialAdapter[T]): Unit = {

    r += ad(e => ()) // error on this line
    ()
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.