How to convert Java generics into kotlin language generics
The java code:
public abstract class Request<T, R extends Request> implements Serializable {}
But the kotlin code:How to do???
How to convert Java generics into kotlin language generics
The java code:
public abstract class Request<T, R extends Request> implements Serializable {}
But the kotlin code:How to do???
According to my needs, I finally realized this way.
Open abstract class Request < T, out R > (url: String): Serializable where R: Request < T, R > {}
Open class GetRequest < T > (url: String): Request < T, GetRequest < T > (url){
}
You can just use the Java-to-Kotlin converter and get
abstract class <T, R : Request<*, *>> : Serializable
But the second Request
(the one in extends Request
) is a raw type which shouldn't be used in Java and is happily unsupported in Kotlin. Very likely it should be Request<T, R>
instead; then the translation uses Request<T, R>
in Kotlin too.
public abstract class Request<T, R extends Request> implements Serializable {}
So, we have two generic types. T
, which can by any type and R
which have to extends Request
. But, Request
still requires two parameters.
In Java it's simpler as generics are not so strict and we can use raw type.
In Kotlin it would be like this:
abstract class Request<T, R : Request<*, *> : Serializable
, but then we have a error: This type parameter violates Finite Bound Restriction.
So, we need to specify somehow second type argument.
I am not sure if it is a correct way for your case, but you can do following:
abstract class Request<T, R : Request<*, R>> : Serializable {
abstract val dummy: R
}
class ExampleRequest<T, N> : Request<T, ExampleRequest<T, N>>() {
override val dummy: ExampleRequest<T, N> get() = this
}