2013-10-02 2 views
0

저는 스칼라 (초보자)와 놀고 있었고 Java 7 NIO를 사용하려고했습니다 (시작하기 쉽기 때문에). 하지만 받아들이 기 위해 CompletionHandler을 인스턴스화하는 방법을 알아낼 수 없습니다. 다음 코드는 잘못하고 나는 그것을 고칠 수 : 인터페이스 유형이기 때문에,스칼라에서 Java 7 완료 핸들러를 인스턴스화하는 방법

... 
val connectionHandler = new CompletionHandler[AsynchronousSocketChannel, Integer] { 
    def completed(result: AsynchronousSocketChannel, attachment: Integer) {} 
    def failed(exc: Throwable , attachment: Integer) {} 
} 
... 

을 그리고 :

connectHandler 인스턴스를 생성하기 위해
package async 

import java.nio.channels.AsynchronousServerSocketChannel 
import java.net.InetAddress 
import java.net.InetSocketAddress 
import java.nio.channels.CompletionHandler 
import java.nio.channels.AsynchronousSocketChannel 

class AsyncServer (port: Int) { 

    val socketServer = AsynchronousServerSocketChannel.open(); 
    socketServer.bind(new InetSocketAddress(port)) 

    val connectionHandler = new CompletionHandler[AsynchronousSocketChannel, Integer](){ 

    } 

    def init() = socketServer accept(1 , connectionHandler) 

} 

답변

2

, 당신은 CompletionHandler 방법을 구현해야 A의 불변하지만, 호출하고있는 방법은에서 contravariant입니다 : 당신이 유형을 캐스팅 할 필요가

... 
public abstract <A> void accept(A attachment, 
           CompletionHandler<AsynchronousSocketChannel,? super A> handler); 
... 

확인 :

socketServer accept(1, connectionHandler.asInstanceOf[CompletionHandler[java.nio.channels.AsynchronousSocketChannel, _ >: Any]] 
관련 문제