Partial class delegation in Kotlin

How can I partially delegate methods / fields to Kotlin?

To be specific: here I am trying to inherit the User class from the TraitA interface and implement the marked: Boolean field in the StateA wrapper. This will clear the User implementation because marked is just a status field. Please note: TraitA cannot be a class because I want to use several of these interfaces: User() : TraitA by StateA, TraitB by StateB, ..

 /* does not compile (Kotlin M12) */ interface TraitA { var marked: Boolean fun doStaffWithMarked() // must be overridable } class StateA() : TraitA { override var marked = false } class User() : TraitA by StateA(){ override fum doStaffWithMarked() { //...all fancy logic here... } } 

An alternative is to implement all in one place:

 class User() : TraitA{ override var marked = false // ugly code override fum doStaffWithMarked() { //... } } 

Is there a way / template that could solve this problem with simple and minimal code? Code / bytecode generation is not an option for me.

UPDATE

I did not quite understand this, but doStaffWithMarked() that doStaffWithMarked() unique to each User .

Therefore, I can offer a "semi-bad" solution with runtime statements:

 interface TraitA { var marked: Boolean /* must be overridden */ fun doStaffWithMarked() = throw UnsupportedOperationException() } class StateA() : TraitA { override var marked = false } class User() : TraitA by StateA() { override fum doStaffWithMarked() { //...all fancy logic here... } } 

The question remains open, since a really good solution would be to verify that doStaffWithMarked() at compile time.

+7
multiple-inheritance delegation kotlin
source share
2 answers

Divide TraitA into two interfaces, then delegate them and implement another:

 interface TraitA { var marked: Boolean } interface TraitAPlus : TraitA { fun isMarked(): Boolean } class StateA() : TraitA { override var marked = false } class User() : TraitA by StateA(), TraitAPlus { override fun isMarked(): Boolean { return marked } } 
+7
source share

Here's a version that only inherits from StateA instead of delegation, but that's not very nice:

 interface TraitA { var marked: Boolean fun isMarked(): Boolean } abstract class StateA() : TraitA { override var marked = false } class User() : TraitA, StateA() { override fun isMarked(): Boolean { return marked } } 

And here is a somewhat unreasonable approach when I delegate TraitA an anonymous instance of StateA

 class User() : TraitA by object : StateA() { override fun isMarked(): Boolean { return marked } } { } 

To be honest, I'd rather rethink the design of the class hierarchy. In particular, you can put method implementations in interfaces (but not property values), therefore, if isMarked() depends only on marked , you can simply set the implementation for it directly in TraitA . Then your code will look like this:

 interface TraitA { var marked: Boolean fun isMarked(): Boolean { return marked } } class StateA() : TraitA { override var marked = false } class User() : TraitA by StateA() { } 

Edit: Divide the answer: fooobar.com/questions/827316 / ...

+1
source share

All Articles