Question

In Kotlin we'll have possibility to create a "trait that may require a class being extended on the call side", like

 class Bar {}
 trait T1 : Bar {}
 class Foo : Bar, T1, T2, T3 {}
 class Wrong : T1, T2 //error: Wrong should extend Bar

I can't imagine any flow where I can apply this structure. Can anyone tell me why we need it?

Was it helpful?

Solution

I think the main reason for this is to allow the trait to make use of methods and properties defined in the concrete class. Imagine that Bar had some basic method that other convenience methods could be built on top of... by having the trait require that it be used on subclasses of Bar, you could define methods in the trait that call that method. You could then provide those methods to many subclasses by giving them the trait.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top