Discover the sights in Kotlin

In Kotlin , we will have the opportunity to create a β€œtag that may require an extension of the class on the call side,” for example

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

I cannot imagine a single thread where I can apply this structure. Can someone tell me why we need this?

+6
source share
1 answer

I think the main reason for this is to allow the attribute to use the methods and properties defined in a particular class. Imagine that Bar had some basic method that could be built by other convenience methods on top ... because the trait requires it to be used in subclasses of Bar , you could define methods in the attribute that calls this method. You can then provide these methods for many subclasses by giving them a trait.

+6
source

Source: https://habr.com/ru/post/903517/


All Articles