Travis Griggs
09/16/2021, 12:30 AMlistOf<DragGesture>(DragCyclePositionGesture.class, DragMoveGesture.class, DragBeginGesture.class, DragEndGesture.class)
is not legal. Number one, there needs to be way a way to make the template not be instances of DragGesture, but rather subtypes of DragGesture? Is that even specifiable? And the compiler isn't happy after the first .class either, i start getting all kinds of red after that. Am I supposed to use companions in this case? I'm not even sure how companions fit in the scheme of inheritance. 😕Joffrey
09/16/2021, 7:06 AMMyClass::class
(not .class
).
Now that being said, if you want to use inheritance so that the classes can tell you something polymorphically (not instances of those classes), then using companion objects seems appropriate because they can implement interfaces, unlike KClass
or Class
Travis Griggs
09/16/2021, 4:07 PMval 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
}
The part I'm not too hip on is the pairs of companion::function and constructor reference.xxx.proximity(this, event)
in the filter and minBy blocksJoffrey
09/16/2021, 5:26 PMGestureFactory<T : DragGesture>
and make the companion objects of each class implement it. It would define the proximity function and factory function in a nicer way I guessTravis Griggs
09/16/2021, 9:44 PMJoffrey
09/17/2021, 12:51 AMlistOf(DragMoveGesture, DragBeginGesture)
(etc.)