Isaac
Isaac

Reputation: 73

Override Java method with complex type in Kotlin

I'm trying to implement JsonHttpResponseHandler#onSuccess. When I use:

fun onSuccess(status: Int, headers: Array<Header>?, response: JSONArray?)

I get an accidental override error, but when I add the override modifier, I get an onSuccess overrides nothing error. Am I doing something wrong, or have I discovered a bug in the Kotlin compiler?

Upvotes: 1

Views: 3116

Answers (2)

Jayson Minard
Jayson Minard

Reputation: 86016

In Kolint M9, you have more flexibility in overriding Java methods about the nullability. This is one of the things people get wrong the most, not seeing that the Java parameters might be NULL from Kotlin's perspective. In M9 you can receive the parameters how you wish Kotlin to treat the Nullability rather than how they are actually seen from Java perspective. So if you know the parameter is never null, you can receive it without the ?. Previously that would result in a mismatch for checking if you actually overrode the method and give an error, now it is considered equivalent.

Upvotes: 2

Andrey Breslav
Andrey Breslav

Reputation: 25787

Try using "Override Methods" (^O on Mac), it will suggest the correct signature.

I suppose that it is

fun onSuccess(status: Int, headers: Array<out Header>?, response: JSONArray?)

but I'm not exactly sure.

Upvotes: 5

Related Questions