Is it possible to define a function which expects ...
# announcements
r
Is it possible to define a function which expects a type which has implemented
componentN()
=> https://kotlinlang.org/docs/reference/multi-declarations.html
🤔 1
b
Seems unlikely since
componentN
is defined differently based on type and each
componentN
shares no common ancestor. That only leaves reflection as a possible solution...
r
I hoped there is some hidden interface or such
k
You can create an Interface which expects the function
Copy code
interface Component<T> {

    operator fun component1(): T
}
r
that's not what I want though as I cannot add interfaces to classes I don't own
d
What you want is structural typing / duck typing and it's not supported by Kotlin.
r
I don't want it to work for arbitrary methods, I am only looking for a solution of
componentN
actually I am looking for a solution to say any
data class
but I doubt that there is something and hoped for
componentN
d
That's still structural typing
r
in a general sense yes but depending on the implementation of kotlin it doesn't need to be
d
And there is good reason for this,
componentN
has no meaning on it's own. What does this component do? Why exactly component1? Why not 2? It only makes sense attached to a specific type.
Instead you should accept an interface that requires certain `val`s.
r
you can destructure any type which has
componentN
implemented, without any interface involved and this makes sense
and I would like to reuse this behaviour
d
Yes, but only if you have the actual type at hand. There is no interface involved, because that interface would be meaningless. What do you do with some type that only offers
componentN
functions?
r
something like:
Copy code
fun <T: WithComponent1<R>, R> foo(t: T): R = t.component1()
d
Yes, but that... provides no value. You have no idea what
component1
does in this case. It could produce a username, the amount of items in a data structure, an average, whatever.
r
I guess I am thinking on a more abstract level. You could compare two component1() calls of different types for instance.
d
Yes, but that makes no sense. Those two component1-s have no semantic relation to each other except that they happen to be the first item in their respective data class
r
it does for me 😉 I don't want to convince you that it makes sense for you though and I see that you cannot help me in this case. Thanks for your time anyway
d
I don't see how it does make sense 😄 Do you have a concrete example in where you would like to use this?
k
So basically you want to express, that you take any type as long as it has a certain
componenN
method, but without using an interface? I don't know any mechanisms in Kotlin that support that.
g
There is no way to do that without reflections and I agree with Take, this is implementation detail that shouldn't be used as generic data accessor, instead concider to use some provider, like compareLazy(instanceA::propertyA, instanceB::propertyB)
It would allow to define this code truly generic way and do not depend on any interface, inheritance or class implementation detail
4