Having a class that implement a interface with generic type of "This" class
Is there a way to do this without a cast?
Simple code:
interface Triggerable<This: Triggerable<This>> { var trigger: (This) -> Unit fun triggerNow() = trigger(this as This) } class Test : Triggerable<Test>{ override var trigger: (Test) -> Unit = { /*...*/ } } The same a little more complex:
interface TriggerInterface<T> { val trigger: (T) -> Unit fun triggerNow() } interface Triggerable<T: Triggerable<T>>: TriggerInterface<T> { override fun triggerNow() = trigger(this as T) } interface Signalable<T>: TriggerInterface<T> { var value: T override fun triggerNow() = trigger(value) } class Test : Triggerable<Test>{ override val trigger: (Test) -> Unit = { /*...*/ } }