I keep finding trait
mentioned in Kotlin examples. For example this answer uses trait
.
Is the trait
keyword used in Kotlin? If it is, where can I find info on it, and if it is not, was it part of the language spec at some point?
In Kotlin, interfaces are traits (historically even keyword trait was used instead of interface ). In practice, it means that we can define default bodies for methods and declare properties (but without any actual values so they still have to be overridden in a non-abstract class): interface NumberHolder {
Interfaces Interfaces in Kotlin can contain declarations of abstract methods, as well as method implementations. What makes them different from abstract classes is that interfaces cannot store state. They can have properties, but these need to be abstract or provide accessor implementations.
The keyword trait
was a keyword in Kotlin but now it's removed. It was deprecated on the Kotlin M12 release.
You can find more here.
Edit: It was replaced by the interface
keyword, see Traits are now interfaces in the notes to M12 release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With