2011-08-03 2 views
1

다음 코드가 있지만 컴파일되지 않습니다. 문제는 내가 T가 Event의 서브 클래스 여야한다는 것입니다.스칼라의 제네릭 함수에 대한 도움말

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

는 당신이'수입 scala.swing.event 등의 필요한'import'-문을 포함하는 경우, 그것은 매우 도움이된다 ._' 그것은 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 
    () 
    } 
} 
관련 문제