Java has the @Override
annotation. This annotation, when applied on a method, basically says that this method is intended to be an override of a superclass method. If the annotation is applied on a method that does not share signatures with any method in the superclass, the Java compiler gives an error.
When Kotlin came along, Override
was promoted from an annotation to the keyword override
.1 Conversely, Java's volatile
keyword, which signified an uncached variable2, has its counterpart in the @Volatile
annotation in Kotlin. The same situation applies to synchronized
, transient
, and I think a few more. Additionally, synchronized
blocks (not methods) aren't even a core part of Kotlin; they are a function taking a lambda (which looks very much like block syntax in Kotlin).
Another notable example is Python. when dealing with class-based programming, a lot of Python's features are accessible through annotations3. For example, static methods are annotated with @classmethod
4, whereas Java uses the static
keyword and Kotlin uses a companion object
, both syntactic features. A similar situation occurs with @dataclass
, with the Kotlin and Java equivalents being data class
and record
, respectively. To get abstract classes and enums in Python, you have to extend a class found in the standard library, while they are both core syntactic features in Java and Kotlin.
This kind of game where you either put something as a core syntax feature or a part of the standard library intrigues me. I imagine Rust could have made a deriving
keyword or use the colon syntax (as it already does with traits) instead of the derive
macro (which is similar enough to annotations). What considerations must be taken into account when determining what features go into the standard library versus what makes it as a dedicated syntactic feature?
1As a matter of personal preference, I prefer the annotation.
2As far as I can tell.
3Actually called decorators, but I'll call 'em annotations for the sake of not confusing people.
4Yes, I know about @staticmethod
, but @classmethod
is closer to the Java/Kotlin equivalents