I have a polymorphic class tree that provide some ...
# android
t
I have a polymorphic class tree that provide some different drag behaviors. I was trying to put something together that allowed me to query the different candidate class types for which is more appropriate to use. What my brain wanted was first class classes with methods. But that's not a thing in this world, I don't think? So I came up with the following:
Copy code
val candidates = listOf(
   Pair(DragCyclePositionGesture::proximity, ::DragCyclePositionGesture),
   Pair(DragMoveGesture::proximity, ::DragMoveGesture),
   Pair(DragBeginGesture::proximity, ::DragBeginGesture),
   Pair(DragEndGesture::proximity, ::DragEndGesture)
)
val filtered = candidates.filter { it.first(this, event).abs <= tolerance }
return filtered.minByOrNull { it.first(this, event).abs }?.let { (_, constructor) ->
   val gesture = constructor()
   gesture.loadFrom(this)
   gesture
}
Is there some idioms/reflection/something I could use that would make this easier? In particular the pairs of companion methods to class constructors. I would have rather just enumerated class objects, scoring them on proximity, and then instantiating the best one.