0

I want to declare a function with a generic type which has to inherit from another generic type that should be an interface, and some other class. But Kotlin complains and doesn't let me do this.

class SomeClass<T: SomeInterface> {

    fun <R>someFunktion(task: R) where  R : SomeOtherClass, R : T {}
}

If I just replace the generic type T in the function declaration with SomeInterface, it works.

class SomeClass<T: SomeInterface> {

    fun <R>someFunktion(task: R) where  R : SomeOtherClass, R : SomeInterface {}
}

So it seems the problem is that Kotlin does not know whether T is an interface or a class. If it would be a class it could not work because Kotlin does not support multi inheritance for classes. So does someone know a solution? Thank you already in advance for every effort.

1 Answer 1

1

Writing your above code example results in the Kotlin compiler to complain:

Type parameter cannot have any other bounds if it's bounded by another type parameter

It says that you cannot have your variable R be bound by a second type parameter T. However, for whatever reason, the problem can just be suppressed: If you add the annotation

@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
fun <R>someFunktion(task: R) where  R : SomeOtherClass, R : T {}

then Kotlin will stop complaining and everything works just fine.

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

Comments

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.