2011-08-03 8 views
1

次のコードがありますが、コンパイルされません。問題は、TがEventのサブクラスでなければならないことを伝える必要があると思いますか?Scalaのジェネリック関数のヘルプ

scala.swing.Reactions.Reactionの種類は、部分写像[イベント、ユニット]

エラーがある: 型の不一致。パーシャル関数[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 
    } 
} 
+1

であるあなたが '輸入scala.swing.eventなどの必要な' import'-文を、含まれている場合、それは非常に便利ですそれは5行増えるかもしれませんが、100人のユーザーの代わりに、どのインポートが必要かを調べようとしています...それはちょうどそのような時間の無駄です。 –

+0

ごめんなさい。私はインポートとクラス宣言で質問を更新しましたので、簡単に再現できます。 – mentics

+0

これは良い質問です。 :) –

答えて

2

adは、PartialFunction [MousePressed、Unit]を返します。 Reactoins + =はPartialFunction [Event、Unit]の反応を期待しています。 PartialFunction [MousePressed、Unit]はPartialFunctionとして認識されません[イベント、ユニット]

ただ、復帰型をリアクションにしてください。ここでは(反応性タイプなし)コードエラーを再現しようとするすべての人については、

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