why Kotlin decided to have data type to be specifi...
# getting-started
a
why Kotlin decided to have data type to be specified after variable name? this has some limitations, for example code completion with java IDEA suggests you a variable name based on the type, but in Kotlin type is unknown at the moment when variable name is specified..
r
with Kotlin IDEA often suggests type based on variable name
👍 1
i
IDEA suggests to complete both variable name and type based on first several chars of its name.
t
This order also has benefits. • the name is usually more important than the type • names start in the same code column, so it's easier to read a list of all variables • if your declaration starts with long type (happens quickly with generics) this would sort of hide the actually interesting name • in many cases the explicit type declaration can be omitted, which would lead to inconsistencies in you code if type declaration came first.
💯 7
👍 5
m
Also worth saying for historical reasons that this type placement decision has been influenced by Scala.
n
In addition to what's been said (esp. the name being more important than the type), I also find the order helpful because I switch back and forth between Kotlin and Typescript. It's easier to come back to Kotlin after a while on the front end because the typing syntax is so similar.
j
Type inference, which I say makes it worth it
👍 3
a
thank you guys for explanation, pretty clear now.. but still, how name completion works in this case? having Java I type:
MyClass
then just ctrl+space and IDEA suggest
myClass
as var name.. but with Kotlin:
var
.. and then happens nothing, right? so type inference is a price paid for names autocompletion..
n
If I type
var s
into IntelliJ and hit Ctrl+Space, I get suggested to me names based on every single class starting with
s
(beginning with
string
and going on down into less frequent names).
a
isn't that too excessive?
t
I’ll trade auto completing a name based on type for readability any day. Also, there are more instances where an IDE will suggest a poor name than there are it will suggest a good name. For example, how often do you want to name a variable string or int? That doesn’t describe what it does at all. Trying to understand when the auto suggested names are really that helpful? But I also do a lot of iOS. Switching back and forth from Kotlin and Swift is so much nicer than Swift and Java. 😂 That’s personal though ha.
1