Is there a way to define a function in an interfac...
# getting-started
a
Is there a way to define a function in an interface with a generic or Any parameter and constrain it to a more specific type in a specialization of that interface? E.g.
Copy code
//Generic
    interface Model<T> {
        fun <K : Comparable<K>> find(key: K?): T?
    }
or
Copy code
interface Model<T> {
        fun find(key: Any?): T?
    }
Specific: the key for a
Thing
will always be a
String?
, but another specific case could be an
Int?
Copy code
interface ThingModel : Model<Thing> {
        fun find(key: String?): Thing?
    }

    interface ThangModel : Model<Thang> {
        fun find(key: Int?): Thang?
    }
I intend the implementations to return the result from memory when available, and to query a db if not, otherwise I would just use the built in
find(predicate: (T) -> Boolean)
At the moment if I just do implementations of the generic interfaces I can compile the following:
Copy code
val m = ModelImpl()
    m.find(0)
    m.find("")

class ModelImpl : ThingModel
I want this to be an error, that is I want implementations to be constrained to one type of, for instance, either String or Int.