TareK Khoury
TareK Khoury

Reputation: 13021

Why didn't Kotlin follow Java syntax?

I'm in the process of learning Kotlin as an Android developer!

Out of curiosity, why didn't the JetBrains guys follow the Java style syntax (where they could have), and made it easier on developers to learn Kotlin?

For example, defining a simple function in Kotlin:

fun simpleFunc(x: Int): Int {

// do stuff

}

Why didn't they do:

fun Int simpleFunc(Int x) {

// do stuff

}

I would appreciate hearing your opinion on this

Upvotes: 2

Views: 412

Answers (3)

user6426085
user6426085

Reputation:

Java is like a coffee, and Kotlin means that coffee with a little bit sugar in there. In some cases, Kotlin does increase the efficiency and make the programming more enjoyable.

Comparing with Java, Kotlin is more effective and actually can work with Java pretty well. Check the example in that picture here about Safe Calls on the official kotlinlang.org,

enter image description here

In Chains, when there's a null value,you need to use if function to determine whether the value is null,but there's only one sentence method needed for Kotlin.

Plus, when you are using Gradle daemon and Smart Compilation,Kotlin shows a faster compile speed than Java.

enter image description here

the horizontal axis means ten consecutive incremental builds with one core file changed.

You can see that the Gradle daemon still takes two or three runs to warm up, but after that the performance of both languages is very similar. With no changes, Java takes 4.6 seconds per warm build, while Kotlin averages 4.5 seconds. When we change a file that isn’t used by any other files, Java requires an average of 7.0 seconds to do a warm build, and Kotlin clocks in at 6.1. And finally, when we change a file that is imported by many other files in the project, Java requires 7.1 seconds to do an incremental build once the Gradle daemon is warmed up, while Kotlin averages 6.0 seconds.

Citations: 1. https://kotlinlang.org/docs/reference/null-safety.html

  1. https://medium.com/@johnkorly/kotlin-vs-java-performance-drill-down-which-to-choose-2514bdf91916

Upvotes: 3

Lukas Lechner
Lukas Lechner

Reputation: 8191

The kotlin team describes here why the type declarations (like in your example) are on the right:

Why have type declarations on the right?

We believe it makes the code more readable. Besides, it enables some nice syntactic features. For instance, it is easy to leave type annotations out. Scala has also proven pretty well this is not a problem.

Upvotes: 2

yole
yole

Reputation: 97188

As mentioned in the Kotlin FAQ, Kotlin's syntax makes it more natural to omit type declarations when they can be inferred by the compiler (which isn't supported by Java). Also, from our experience with Kotlin, we see no evidence that Kotlin's type declaration syntax presents a difficulty for people learning Kotlin.

(Note that your suggested syntax is also different from Java, so it's not clear why you think it would be easier to learn.)

Upvotes: 14

Related Questions