Methods are declared with the fun
keyword in Kotlin, whereas most other languages use def
(e. g. Scala, Python) some use fn
(e. g. Rust) or function
(JavaScript). I wonder whether it was intended or at least a welcome sideeffect to have the word “fun” in the language Was it?
It took me a while to get used to fun
, and I would have preferred the straight-forward function
. (If override
and companion object
aren’t considered too long, function
shouldn’t either, even though it’s used more often.) One nice aspect (if you will) is that fun
, val
, and var
all have the same length, but I don’t know if that played a role in the decision.
PS: JavaScript uses function
.
I don’t think there’s any word for defining functions that “most” languages use. We use “fun” because we like it - and yes, we do know what the word means in English.
I think there is some masterful psychological manipulation going on here. Every time you write a function in Kotlin they want you to believe you’re having fun.
I mean, think about it… all the awesome stuff they’ve done with the language; it’s like they want you to enjoy yourself while programming. Having fun while programming, absurd.
Should have used function, func or fn, just saying, it would have been a better larger audience choice.
SML is using fun. The audience coming from there has no quantitative relevance… Quality over quantity in this case
I heard in the recording of some conference that fun
is used because it’s 3 letters long like var
and val
.
Ok, what about packages that use the word fun? I’m running into a problem right now since my Android app contains the word fun, as in package fun.domain.myApp. How do we get around this obvious problem?
What is your problem exactly? As long as you use IntelliJ/Android Studio, you should not even notice this problem, because IDE solves it for you automatically - by enclosing a reserved word in backticks:
package `fun`.domain.myApp
import `fun`.domain.myApp.MyClass
ML and Erlang both use “fun” keyword (Erlang for lambdas), Go uses “func”. So it is not something new, and superior to alternatives you mentioned.
- Fn - cryptic
- Def - unspecific to functions (define what?)
- Function - overly verbose