Hello guys!
I have been using Kotlin for the past month and I absolutely love it (maybe too obsessed with it too)
I really like the syntax and it definitely has boosted my productivity by quite a bit.
However, there is one inconsistency in the syntax of Kotlin that in particular irks me, which is the syntax for denoting the return type of a function.
In function declarations, we use the :
operator to denote the return type like so:
// function declaration
fun myFunction(a: Int, b: Char): Boolean {
...
}
However, the return type of a function in a function type is denoted using the ->
operator like so:
// function type
val myFunction: (Int, Char) -> Boolean
Now, I read from this Kotlin webpage that the choice to use :
in function declaration and ->
in function type is to improve code readability, as illustrated in the following example from the webpage above:
// before:
fun max(col: Collection<Int>, compare: fun(Int, Int): Int): Int {
...
}
// after:
fun max(col: Collection<Int>, compare: (Int, Int) -> Int): Int {
...
}
I totally agree that it makes it more readable and I appreciate that. However, I believe that if we instead use the ->
in function declarations, we can achieve a similar level of readability (in my opinion) while maintaining consistency, like so:
fun max(col: Collection<Int>, compare: (Int, Int) -> Int) -> Int {
...
}
If the above suggested syntax is used, consistency is improved like so:
fun myFunction(a: Int, b: Char) -> Boolean {
...
}
val myFunction: (Int, Char) -> Boolean
This makes perfect sense if we also consider that currently :
is being used for denoting a type for a variable while ->
is used for denoting a return type in function types as well to denote returning expressions in lambda functions. The type of myFunction
is not Boolean
, it is (Int, Char) -> Boolean
, so using :
to denote a return type for a function does not seem logical to me.
I think this is also used in Swift too (correct me if I am wrong).
I understand that it is nearly impossible to change the syntax now that Kotlin has already been released and is used in production; I also do see how trivial this inconsistency is and that there are more important issues at hand with the Kotlin development. However, I still want to write my thought about this since I love this language so much and I want to see if there is anyone out there that shares the same sentiment as me