https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Alexey Anufriev

07/14/2020, 11:52 PM
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

Robert Jaros

07/14/2020, 11:58 PM
with Kotlin IDEA often suggests type based on variable name
👍 1
i

ilya.gorbunov

07/15/2020, 12:30 AM
IDEA suggests to complete both variable name and type based on first several chars of its name.
t

Tobias Berger

07/15/2020, 7:10 AM
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

Matteo Mirk

07/15/2020, 3:51 PM
Also worth saying for historical reasons that this type placement decision has been influenced by Scala.
n

nschulzke

07/15/2020, 5:06 PM
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

jordigarcl

07/15/2020, 6:20 PM
Type inference, which I say makes it worth it
👍 3
a

Alexey Anufriev

07/17/2020, 10:03 PM
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

nschulzke

07/17/2020, 10:20 PM
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

Alexey Anufriev

07/18/2020, 12:04 AM
isn't that too excessive?
t

TJ Olsen

07/25/2020, 3:54 PM
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
2 Views