hi guys, i have a simple question, i’ve overrided ...
# announcements
r
hi guys, i have a simple question, i’ve overrided a class that exposes a method
Copy code
protected Class getPrototypeClass(T content) {
With this code
Copy code
override fun getPrototypeClass(content: AlfredElement): Class<*> {
        return when (content) {
            is Category -> AlfredCategoryRenderer::class.java
            is Suggestion -> AlfredSuggestionElement::class.java
        }
    }
And AlfredCategoryRenderer is a class with this type
Copy code
: Renderer<AlfredElement>()
So, how can i tell kotlin compiler something like i can do i java which is tell the method getPrototypeClass will return a class that is Something like extends AlfredElement?
v
m
I am not sure it this is what you mean, but I would return covariant type:
Copy code
sealed class AlfredElement
class Category : AlfredElement()
class Suggestion : AlfredElement()

abstract class Renderer
class AlfredSuggestionElement : Renderer()
class AlfredCategoryRenderer : Renderer()

fun getPrototypeClass(content: AlfredElement): Class<out Renderer> = when (content) {
    is Category -> AlfredCategoryRenderer::class.java
    is Suggestion -> AlfredSuggestionElement::class.java
}
It means that
getPrototypeClass
returns Class of something that is subtype of
Renderer