Feature Request: Removing parentheses for for-loop


#1

Dear Jetbrains,

Kotlin has become my main choice for almost every Java project I have at school, but I think a great language such as Kotlin can be even greater if you can change its syntax a bit.

What I do not like about Kotlin is the For Loop. It should be better to remove the parentheses like in Swift. In the old Java, I feel comfortable with the parentheses since the C-style for-loop in java is a lot more powerful. The for-loop in Kotlin is, on the other hand, just too plain and simple to put in brackets.

Not in this topic, but I think that Kotlin compilation time is not bad. You should pay a bit more attention to the execution time. Kotlin bytecode seems ‚Äėsignificantly slower than Java bytecode‚Äô for even simple program like for loop a billion times (If you subtract the fix ‚Äėadditional constant time‚Äô, I do not know how to call it)

Updated: Just by changing all the forEach, repeat, filter, reduce… stuff to pure loop, my program gains an unprecedented 150% improvement in speed, and what is more, the old version is even slow compared to an equivalent JavaScript program. Note that my I am just a university student and my project may be biased but it is true that Kotlin has a problem with performance.

Thank you for creating Kotlin.


#2

There was a long discussion about similar request here.
My own opinion is that it is a bad idea (event ignoring the fact that it is not possible in kotlin due to syntax ambiguity). Currently all control constructs in kotlin like if, while and for are almost equivalent of global functions and it is a good thing since its limits the number of language constructs. I would event go further and propose to replace for(a: collection){} with for(collection){a->} to make it completely like function, but this is not necessary.


#3

I’ll add to that that idiomatic Kotlin code rarely uses for loops. Kotlin Standard library provides a variety of high-level utilities to close everyday programmer’s needs.


#5

By idiomatic code, do you mean the use of functional programming features such as map or foreach. If that is the case, then it will be a big disadvantage as the former is ‚Äúextremely slow‚ÄĚ and the latter is still ‚Äúsignificantly slower‚ÄĚ than a for-loop. Can you recommend me a standard utility from Kotlin that does not involve any performance problem?


#6

I agree it is not necessary, but it will make Kotlin syntax much nicer :smiley:


#7

It is a well known fact that in Java usage of lambdas have some performance issues because of unavoidable creation of anonymous class instances for lambda , but if you look through the code of forEach function in a standard library, you would see that it have inline modifier, it means that compilers does not actually create anonymous class for lambda in this case. Of course, I am not sure, about performance issues in each specific case, but you need to measure it yourself.

As for syntax, I am a Pascal guy initially, so I am quite familiar with bracket-less syntax, but in my opinion, the kotlin syntax is probably the best one I’ve ever tried. You can always be sure that something in round brackets is a parameter and something in curly braces is a body or implementation.


#8

While it is true that Kotlin (and the RetroLambda project) currently converts lambdas to anonymous inner classes, that actually is not true with Java 8 lambdas. Java 8 makes use of the invokedynamic instruction for better performance. See https://www.infoq.com/articles/Java-8-Lambdas-A-Peek-Under-the-Hood if you want to dig into the gory details.

For example a non-capturing lambda is actually ‚Äúdesugared‚ÄĚ to a static method not an inner class on Java 8.

Hopefully someday in the near future Kotlin will also use invokedynamic, but as you said in most cases lambdas are inlined and there is basically no overhead at all.


#9

I do understand about invokedinamic. The problem is that stream processing performance is still about 4 times worse than for-loop in java 8 (most of it comes not from lambdas, but from lack of additional optimization inside lambdas). I believe that previous comment about performance was caused by that.


#10

Optimizing (1..3).forEach{...} would be nice (it creates an IntRange and an IntIterator).


#11

True enough, Kotlin Syntax is possibly one of the best one out there, even comparable to Python or Ruby or Matlab. In my opinion, the only one that may beat it is Swift but Swift is, unfortunately, not as cross-platform as Kotlin and does not have as good integration with an existing popular language as Kotlin to Java. Therefore, I suggest that Kotlin team should try following some of Swift syntax rules because the combination of the two may give birth to the best language ever. My suggestion about removing parentheses from for loop is inspired by Swift too.

Naturally as a student, my knowledge of programming language design is in no way comparable to that of yours so I may not understand the reason behind these parentheses of for-loop like I cannot understand why a language with terrible syntax like Java has become so popular. Was it not for the Jar Feature, I would never bother myself with Java. Kotlin, however, changed everything. Java to me is a dead language, but Kotlin to me is a super nice language with a lot of room for improvement. With some more improvements at syntax (like Swift) and performance (like Fantom), I strongly believe that Kotlin will one day erase the existence of Java and even rival Python in terms of code beauty or even Matlab, Julia and R in scientific computing domain (of course it needs some more packages for that).