2011-08-03 35 views
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 
    } 
} 
+1

對於所有這些,誰試圖重現錯誤,這是非常有幫助的,如果你包括需要'import'語句,比如'進口scala.swing.event 。「可能還有5條線路,而不是100個用戶,試圖找出哪些進口是必要的...... - 這只是浪費時間。 –

+0

對不起。我更新了導入和類聲明的問題,因此可以輕鬆地進行轉換。 – mentics

+0

現在這是一個很好的問題。 :) –

回答

2

廣告返回PartialFunction [的mousePressed,單位]。 Reactoins + =期望反應是PartialFunction [Event,Unit]。 PartialFunction在其第一個類型參數中是不相容的,因此PartialFunction [MousePressed,Unit]不被視爲PartialFunction [Event,Unit]

只需將adreturn類型設置爲Reaction。下面是代碼(沒有反應型)

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 
    () 
    } 
}