1

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???

3
  • "public abstract class Request<T, R extends Request> implements Serializable",how to convert this use kotlin language? Commented Sep 30, 2019 at 1:43
  • test this >>> something: String Commented Sep 30, 2019 at 1:50
  • What's the question here? What have you done so far? Commented Sep 30, 2019 at 2:56

3 Answers 3

2

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){

}

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

"This type parameter violates Finite Bound Restriction."
That's the error compiler throws when you try use two wildcards. Actually, I am not convenient that there should be Request<T, R> - I see some use cases where second type parameter should be in Request bounds.
Thank you very much for your help. According to my needs, I finally achieved this. 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){ }
0

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
}

1 Comment

Thank you very much for your help. I learned some new knowledge from your help. 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){ }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.