Logic behind colon ":"

When we declare “var x:Int”, the variable x is inheriting its properties from Int. So the property is flowing from right to left. But there are other examples where the property seems to flow from left to right. If I use “constructor(a: String) : this(a,12) {…}”, the first constructor is invoking the second constructor, which is in the opposite direction. I am new to Kotlin, and I am trying to wrap my head about how to contextualize the use of colon.

1 Like

It’s cool to see your perspective on the flow of definitions of things in the language.

Personally, I prefer to use the language of “x is declared/defined/exposed/returns an Int”. Saying x “inherites” from Int makes it seem like x is part of the type ontology instead of a reference or lens that exposes an instance.

IMO some of the primary reasons for the token name on the left and the type on the right are practical usage of having names aligned (yay no column alignment!) and enabling easier use of language features such as type inference.

You might find this interesting.

2 Likes