```interface Criteria<T: Criteria<T>> ...
# getting-started
j
Copy code
interface Criteria<T: Criteria<T>> { fun test(): T }

open class Filter1(val data: String = ""): Criteria<Filter1> {
    override fun test() = Filter1("Basic")
}

object Standard: Filter1() {}

// How can I provide some correct type here?
fun whatever(): Criteria<*> {
    // Some custom code...
    return Standard
}

fun main() {
    println(whatever().data) 
}
Could someone help me with my
whatever
function here, so it returns the correct type? (playground: https://pl.kotl.in/z4lPq_kWE)
a
It looks like you have a recursive interface declaration.
Copy code
interface Criteria<T: Criteria<T>>
Is that intentional?
j
Yes
a
How does that work? It seems infinitely recursive.
j
Just makes it possible to give a generic type of itself to a interface. Just like shown with the
Filter1
type.
y
This works:
Copy code
fun whatever(): Criteria<Filter1>
However, you still won't be able to do
.data
though. That's because a
Criteria<Filter1>
is not guaranteed to be of type
Filter1
You could instead just do:
Copy code
fun whatever(): Filter1
j
My actual code has multiple implementations of
Criteria
. So it's important that it's not only supporting the
Filter1
.
y
Well, how is
whatever
meant to decide what its return type is though? As in, you can't access
.data
on an arbitrary
Criteria<*>
. Is whatever meant to have a type parameter then?
j
I have a complex repository pattern mixed with dependency injection. Where all repositories extend an interface `interface Repository<C: Criteria<C>>.`I want to create a function that can return a type of
Repository<Criteria<*>>
, but I can't get this part to work.
y
And do you expect the callsite to be able to access class-specific properties like
.data
? Because if so there is kind of a solution