2017-04-05 8 views
3

私はうまく動作するJavaクラスファイルを持っていますが、私がKotlinに変換すると問題があります。ここではJavaのバージョンkotlinに変換されたjavaクラスファイルがコンパイルエラーを起こす

public class CallbackWrapper<T> implements Callback<T> { 
    private Wrapper<T> wrapper; 

    public CallbackWrapper(Wrapper<T> wrapper) { 
     this.wrapper = wrapper; 
    } 

    public void onFailure(Call<T> call, Throwable t) { 
     wrapper.onResult(t, null); 
    } 

    public void onResponse(Call<T> call, Response<T> response) { 
     wrapper.onResult(null, response); 
    } 

    public interface Wrapper<T> { 
     void onResult(@Nullable Throwable t, @Nullable Response<T> response); 
    } 
} 

があり、これは、私は何の問題もなく、このクラスを使用している方法です...

request.enqueue(CallbackWrapper<RegisterResponse> { throwable, response -> 
    response?.let { callBack.onResponse(response.body() ?: RegisterResponse()) } 
    throwable?.let { callBack.onFailed(throwable.message!!) } 
}) 

そしてここでは、Kotlinのバージョンは

class CallbackWrapper<T>(private val wrapper: CallbackWrapper.Wrapper<T>) : Callback<T> { 

    override fun onFailure(call: Call<T>, t: Throwable) { 
     wrapper.onResult(t, null) 
    } 

    override fun onResponse(call: Call<T>, response: Response<T>) { 
     wrapper.onResult(null, response) 
    } 

    interface Wrapper<T> { 
     fun onResult(t: Throwable?, response: Response<T>?) 
    } 
} 

変換した後で問題は、Kotlinに変換した後で、私がこのクラスを使用しているコードブロックが機能していないことです。私はそれらの違いは何か分かりません。ここで

は、あなたが私の知る限り、あなたのコードには2つの問題があり、エラーログ

Error:(17, 63) Type mismatch: inferred type is (???, ???) -> [ERROR :<ERROR FUNCTION RETURN TYPE>] but CallbackWrapper.Wrapper<RegisterResponse> was expected 
Error:(17, 65) Cannot infer a type for this parameter. Please specify it explicitly. 
Error:(17, 76) Cannot infer a type for this parameter. Please specify it explicitly. 

答えて

3

をコンパイルしています。

Kotlinはラムダを使ってKotlinの機能インタフェースを実装することができないため、ラムダは機能しません(Javaには適切な関数タイプがないため、Java機能インタフェースの実装が可能です)。だから、そのままのコードを呼び出すために、あなたが代わりにオブジェクトの表記法を使用したい:

request.enqueue(CallbackWrapper<RegisterResponse>(object : CallbackWrapper.Wrapper<RegisterResponse> { 
     override fun onResult(throwable: Throwable?, response: Response<RegisterResponse>?) { 
      response?.let { callBack.onResponse(response.body() ?: RegisterResponse()) } 
      throwable?.let { callBack.onFailure(throwable.message!!) } 
     } 
    })) 

あなたがラムダを使用して好む場合は違った自分のCallbackWrapperクラスを宣言することができます。

typealias Wrapper<T> = (t: Throwable?, response: Response<T>?) -> Unit 
class OtherCallbackWrapper<T>(val wrapper: Wrapper<T>) : Callback<T> { 
    override fun onFailure(call: Call<T>, t: Throwable) = wrapper.invoke(t, null) 
    override fun onResponse(call: Call<T>, response: Response<T>) = wrapper.invoke(null, response) 
} 

だからのbut CallbackWrapper.Wrapper<RegisterResponse> was expected 一部世話です。

これはまだ動作しませんが、エラーメッセージの最初の部分がinferred type is (???, ???) -> [ERROR :<ERROR FUNCTION RETURN TYPE>]であるという理由があります。それは基本的に型推論が失敗したことです。 callBackへの呼び出しは、CallbackWrapperによって暗示されたCallBackインターフェイスとはまったく一致していないようです。

EDIT:

は、より簡潔なソリューションを使用してレトロフィットの質問への答えを追加しました。

関連する問題