Why the reversal of variable name and data type in Kotlin?

Scala, and now Kotlin have put the variable name in front of the datatype and used a colon to separate them, just like Pascal. Just wondering what the rational was for doing this since most statically-typed  languages seem to do it the reverse, and without a colon.

To be type inference friendly, I would say.

I'll expand on this a little bit. Here's a plain old variable declaration + initialization:

var a: Int = 42

You'll note that the compiler can infer the type of the variable from the initializer. You can just delete a certain portion of the code:

var a = 42

If you had types on the left, it wouldn't be this simple.

var Int a = 42   // I bet you wouldn't like the 'var' thing Int a = 42   // this is what I guess you would like to have

var a = 42   // changing ‘Int’ to ‘var’ here?
a = 42           // nope, this wouldn’t work for various reasons


The Go language did one interesting thing: they moved the type from left to right, but didn’t add the colon. Here’s how this would look like:

var a Int = 42

var a = 42   // again, you can just delete a certain part of the code, no need to rewrite anything


I’m not sure I’d personally like this.

1 Like

I think it's a (good) trend. Go also does this (var num int64 = 1234)