Generics Scala中泛型函数的帮助

Generics Scala中泛型函数的帮助,generics,scala,Generics,Scala,我有下面的代码,但它没有编译。我想问题是我需要告诉它T必须是Event的一个子类 scala.swing.Reactions.Reaction的类型是PartialFunction[事件,单位] 错误是: 类型失配;找到:需要部分功能[T,单位]:scala.swing.Reactions.Reaction import scala.swing.event.MousePressed import scala.swing.Component import scala.swing.Reactions

我有下面的代码,但它没有编译。我想问题是我需要告诉它T必须是Event的一个子类

scala.swing.Reactions.Reaction的类型是PartialFunction[事件,单位]

错误是: 类型失配;找到:需要部分功能[T,单位]: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
  }
}

ad返回一个PartialFunction[鼠标按下,单位]。Reactions+=预期反应为部分功能[事件,单位]。PartialFunction在其第一个类型参数中是逆变的,因此PartialFunction[MousePressed,Unit]不被视为PartialFunction[Event,Unit]

只要做a型反应。下面是代码(没有反应类型)


对于所有试图重现错误的人来说,如果您包含所需的
import
-语句,例如
import scala.swing.event.\u
可能需要5行,但不是100个用户,而是试图找出需要哪种导入,…-这太浪费时间了。很抱歉。我用导入和类声明更新了这个问题,所以它可以很容易地复制。现在这是一个很好的问题。:)
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
    ()
  }
}