Fun vs func vs function


#1

Being new to Kotlin and having read several code examples to better understand this language that I rather like, I repeatedly find it strange to see a function definition starting with the keyword “fun”. I know writing code in Kotlin is productive and fun, but seeing the word “fun” appear so often in Kotlin code is distracting and reduces code readability.

“Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs.

Using keyword “fun” to define a function does not make it easier to read the code; it arguably makes it harder as “fun” is a misleading / distracting keyword to most English speakers (IMHO) in the context of program source code. “function” would make it very clear what is being defined, just like the keyword “class” is used in Kotlin to define a class. The proposed keyword “function” could be shortened to “func”, but why shorten it further to “fun”?

“fun” is rather low on the list of appropriate abbreviations for the word “function”. See http://www.abbreviations.com/abbreviation/function

Using “function”’ as a keyword instead of “fun”, fits in rather naturally with other keywords in Kotlin such as “package”, “class”, and “object”.

It just seems like an unfortunate oversight, given the use of words like “object” and “class”, that “function” was overlooked as a keyword in Kotlin.

Has the ship sailed on this request, or could it be accommodated in a future release?

Thanks,

Werner


#2

This ship has sailed many years ago. Introducing any new keyword is a major breaking change that will affect a huge amount of existing code, and we know for sure that Kotlin can be used very successfully with its existing set of keywords.


#3

This is not a stable version of Kotlin, and no compatibility guarantees
are given here: in the future previews of 1.1, syntax, APIs, command-line
switches and anything else may be changed.

Since Kotlin 1.1 allows changes that will break backwards compatibility, changing keyword fun could at least be considered for the next major release, right?. One could deprecate keyword “fun”, but still allow it in 1.1 code, while supporting the new improved replacement. Kotlin is still a young language, it is the right time to make small but important changes to it.

To me it us just poor style to take a word from the English dictionary, in this case the word “function”, and abbreviate it to another word “fun” that is also a proper English word and defined in the English dictionary. The meanings of “function:” and “fun” in the English dictionary are worlds apart and have nothing in common. Again, to me, this makes it highly confusion to read Kotlin code.

Rust abbreviates function to fn, Swift uses abbreviation func, and Ceylon just uses function without abbreviating it.

I think many people will be happy to use “func” as an abbreviation for function. Could this enhancement be incorporated in the Kotlin 1.1 release?

Thanks,

Werner


#4

I found the word fun irritating at first, too. I’d still prefer func, but you get used to fun. It is really not a big deal.


#5

fun already appeared in ML more than 40 years ago https://en.wikipedia.org/wiki/ML_(programming_language)


#6

The meaning of this statement is that newer builds of Kotlin 1.1 may break backwards compatibility with features introduced in earlier preview builds of Kotlin 1.1. We have no plans or intention of breaking compatibility with Kotlin 1.0. The fun keyword was introduced long before Kotlin 1.1.

Once again: Kotlin has been used, successfully, by tens of thousands of developers, who did not see any problems with our choice of keywords and do not find Kotlin code to be confusing to read. We appreciate your feedback, but we do not believe that changing one of the most commonly used keywords in the language would be an improvement or an enhancement.


#7

There even is one upside to the keyword fun over the other alternatives - it has the same number of characters as val and var, making classes just a tad more readable because all the names for properties and functions start on the same column.


#8

It’s a small but beautiful thing to see everything line up so perfectly. The visual combination of that and right-hand types is strangely satisfying to me.


#9

“package”, “class”, and “object” are all okay to have the higher lengths because they’re used relatively rarely. Functions are one if the most commonly created things, so their keyword should be shorter (the old “rule” that common things should be shorter). I would be fine with “func”, but it’s too late. But “fun” is fine. Better than “def” in Python, which is considered a very readable language.


#10

What about fn in Rust ? I think no need to change to function or longer word, fun in Kotlin or def in Scala is just fine


#11

I think that fun its really what we need)


#12

Those who don’t like the fun keyword (if there are still some, one year later) may just avoid it:

val add: (Int, Int) -> Int = { x, y -> x + y }

println(add(2, 3))

#13

That looks more like a 'lamb' rather than a 'fun' :sunglasses:


#14

Kotlin is fun :grin:


#15

Even more fun without 'fun':

val add: (Int) -> (Int) -> Int = { x -> { y -> x + y } 

println(add(2)(3))