It might help to think of abstract
, enum
, data
, annotation
, and value
as modifiers to a class–similar to how you say inline fun
where inline
is the modifier to a function.
Wanting to drop “fun” when writing an inline function is very similar to this request (or do something like Java’s function declarations).
With this change, you effectively take the class
modifier words and turn them into keywords declaring a class. abstract MyClass()
, data MyClass()
, value MyClass()
. You could take this all the way and include visibility modifiers to declare a class, open MyClass()
, internal MyClass()
, and final MyClass()
.
There’s a pragmatic argument that using modifiers over keywords to keep concepts grouped and to protect the language from a flood of keywords. I find it more readable instead of having to translate in my head which modifiers are declaring what things.
IMHO it can make some things slightly harder to learn if you only use modifiers and don’t write the kind of thing you’re declaring.
Small rant on conceptually catagorizing things ¯\_(ツ)_/¯
This is pretty simplified so take it with some salt:
There aren’t just classes (type & protocol & state) in Kotlin, there are lots of other things; interfaces (type & protocol), functions (protocol), fields (state), typealias (type). New coders have to learn more keywords for the same conceptual thing (e.x. "What kind of thing is an “abstract”?, “Oh it’s just a class but has a few extra requirements…”).
One of the most un-class-like modifiers for a class is annotations. Annotations have a lot of restrictions compared to final
, abstract
, or even enum
classes. We can use the concepts of type, state, protocol to figure out what an annotation is.
- Does an annotation define type? Yes… Therefore it isn’t a field or a function
- Does an annotation declare protocol? Yes… it isn’t a typealias either.
- Does an annotation have state? Yes… not an interface.
- It has state+protocol+type; it’s a class!
^ I picked annotations because that is one I think it’s the hardest to conceptually justify. Still it’s just another class–the restrictions added when a class is modified with annotation
, abstract
, enum
differ but in the end, they are all kinds of classes.
Interestingly, Java declares annotations as @interface Foo {}
.