1
我有以下代碼,但它不能編譯。我認爲問題是我需要告訴它T必須是事件的一個子類?Scala中的泛型函數幫助
類型scala.swing.Reactions.Reaction的是PartialFunction [事件,單元]
錯誤是: 類型不匹配;實測值: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
}
}
對於所有這些,誰試圖重現錯誤,這是非常有幫助的,如果你包括需要'import'語句,比如'進口scala.swing.event 。「可能還有5條線路,而不是100個用戶,試圖找出哪些進口是必要的...... - 這只是浪費時間。 –
對不起。我更新了導入和類聲明的問題,因此可以輕鬆地進行轉換。 – mentics
現在這是一個很好的問題。 :) –