``` class Bar<T> protected constructor(type:...
# announcements
c
Copy code
class Bar<T> protected constructor(type: Class<T>) {
    companion object {
        inline operator fun <reified T> invoke(): Bar<T> {
            return Bar(T::class.java)
        }
    }
}

val bar = Bar<String>()
d
Not sure I follow how I would use this.
c
Not sure where the disconnect is. This lets you use
Bar<Type>()
instead of
Bar(Type::class.java)
. Not what you wanted?
d
Yes, that is what I wanted. However I want this to be a companion object.
c
it is?
the
val bar
at the end is just a demonstration.
d
In your case
Bar
has the class parameter. The companion object should have it.
c
It does…
Oh, you don’t want the type parameter on the class?
d
Yes.
c
well, it doesn’t have to be there. but then your parent class won’t know what type of class it’s getting, just that it’s a class
d
data class Person
should have a companion object of type
Model<Person>
, and
Model
needs the
Person
class.
Model
then has methods like
findAll
, which would return all
Person
instances.
c
Type it out as you want it to look, if reified were possible where you want it.
d
class Model<reified T>
I know I can emulate that using factory method. However then I can no longer use it as companion object...
c
oh I see
d
I think delegation using
by
is the best I can do...
c
yup, I think so
I’ve been working on an annotation processor to auto generate interfaces for that purpose actually 😆
d
I am trying to move away from annotation processing 🙂 Otherwise I'd being using querydsl or something. But I am trying to be "kotlin idiomatic" in a way 🙂
c
👍