Donny
07/06/2023, 7:13 PMfun <M: UiModifier, T: Any> M.installDragAndDropHandler(
dndContext: DragAndDropContext<T>,
handler: DragAndDropHandler<T>,
dragItem: T
): M {
onDragStart { dndContext.startDrag(dragItem, it, handler) }
onDrag { dndContext.drag(it) }
onDragEnd { dndContext.endDrag(it) }
return this
}
Does this add the extension function to all UiModifiers?José González D'Amico
07/06/2023, 7:15 PMUiModifier?Donny
07/06/2023, 7:16 PMmohamed rejeb
07/06/2023, 7:17 PMUiModifier all the time but now with generics let's say that you have PrimaryModifier a child of UiModifier , using this ext function on PrimaryModifier will return a PrimaryModifier not a UiModifier , that's the main advantage of using generics here.Donny
07/06/2023, 7:18 PMephemient
07/06/2023, 7:18 PMfun ...(...) = apply {
}
to make it more immediately clear that it always returns thisephemient
07/06/2023, 7:18 PMfun <T: Any> UiModifier.installDragAndDropHandler(
dndContext: DragAndDropContext<T>,
handler: DragAndDropHandler<T>,
dragItem: T
): UiModifier
it would be applicable to all subtypes of UiModifierDonny
07/06/2023, 7:19 PMephemient
07/06/2023, 7:19 PM<M> is that the return type stays narrowmohamed rejeb
07/06/2023, 7:19 PMUiModifierephemient
07/06/2023, 7:20 PMval m1: SomeUiModifierSubtype
val m2 = m1.installDragAndDropHandler(...)
without <M>, m2: UiModifier, so you might need to cast or reorder if you want to call SomeUiModifierSubtype-specific methods on itDonny
07/06/2023, 7:20 PMDonny
07/06/2023, 7:21 PMDonny
07/06/2023, 7:21 PMmohamed rejeb
07/06/2023, 7:23 PMDonny
07/06/2023, 7:24 PMmohamed rejeb
07/06/2023, 7:24 PMephemient
07/06/2023, 7:24 PMDonny
07/06/2023, 7:25 PMopen class UiModifier() {
fun <T: Any> M.installDragAndDropHandler(
dndContext: DragAndDropContext<T>,
handler: DragAndDropHandler<T>,
dragItem: T
): UiModifier {
onDragStart { dndContext.startDrag(dragItem, it, handler) }
onDrag { dndContext.drag(it) }
onDragEnd { dndContext.endDrag(it) }
return this
}
}Donny
07/06/2023, 7:26 PMephemient
07/06/2023, 7:26 PMmohamed rejeb
07/06/2023, 7:27 PMval modifier: CustomModifier = ...
val newModifier = modifier.installDragAndDropHandler() // this will return UiModifierephemient
07/06/2023, 7:27 PMCustomModifiermohamed rejeb
07/06/2023, 7:28 PMDonny
07/06/2023, 7:29 PMDonny
07/06/2023, 7:30 PMmohamed rejeb
07/06/2023, 7:34 PMKlitos Kyriacou
07/07/2023, 8:08 AMfun <M: UiModifier, T: Any> installDragAndDropHandler(
uiModifier: M,
dndContext: DragAndDropContext<T>,
handler: DragAndDropHandler<T>,
dragItem: T
): M {
uiModifier.onDragStart { dndContext.startDrag(dragItem, it, handler) }
uiModifier.onDrag { dndContext.drag(it) }
uiModifier.onDragEnd { dndContext.endDrag(it) }
return uiModifier
}
Would you now find it easier to understand? Well, just because an extension function's syntax looks as if it's part of the class, it doesn't mean it is a part of the class.Donny
07/11/2023, 3:42 PMephemient
07/11/2023, 3:43 PMinternal works in the same module, protected and private are inaccessible