"def" vs. "fun"


#1

I'm not trying to open a can of worms (http://en.wiktionary.org/wiki/can_of_worms) here, but the folks on the very-widely-listened-to JavaPosse raised a reasonable question about the Kotlin keyword "fun" vs. "def" to introduce a function definition.  I thought the question was good enough to warrant at least one airing here.

Scala and Groovy use “def” for functions.  Might Kotlin reduce the impedance mismatch for users coming from those language to Kotlin by using def instead of fun, too?  I don’t feel strongly about it.  Just asking.


#2

The most obvious reason is that "fun" speaks for itslef: you need no manual to find out what it stands for. There are some more reasons.

In Scala and Groovy “def” means something different than “fun” in Kotlin. These languages denote “function or property” by “def”. Groovy even uses “def” for local variables.
In Kotlin “fun” is strictly a function.

One other thing is that “def” stands for “definition”, which a “def” sentence might not necessarilty denote: it may denote a declaration instead.


#3

Hi Andrey,

Any reason why we couldn’t have just one keyword for both concepts?

I don’t see much user benefit to having two keywords (Scala fell in the same trap in a worse way with the equal sign and also :Unit, it would be nice not to repeat that mistake).


Cédric


#4

Any reason why we couldn't have just one keyword for both concepts?

One big reason: pedagogical. Don't name apples grapes, for they are apples, not grapes.

I don't see much user benefit to having two keywords (Scala fell in the same trap in a worse way with the equal sign and also :Unit, it would be nice not to repeat that mistake).

Could you explain this? Thanks

#5

I realize I misread the discussion and/or the documentation, I was under the impression that fun was used to declare functions and def for void methods. I tried to find the meaning of these two keywords in the documentation but came up empty.

fun is for introducing functions, what is def for? Is it even a valid Kotlin keyword? The only traces I found were in comments on the wiki, so obviously not trustworthy.

Side remark: the search feature of this web site should not produce results from comments, it introduces too much confusion. When I’m looking for something about the language, I only want to search through the official reference manual, not random comments by people who are arguing about hypothetical stuff.


#6

fun is for introducing functions, what is def for? Is it even a valid Kotlin keyword? The only traces I found were in comments on the wiki, so obviously not trustworthy.

"def" is not a keyword in Kotlin.

Side remark: the search feature of this web site should *not* produce results from comments, it introduces too much confusion. When I'm looking for something about the language, I only want to search through the official reference manual, not random comments by people who are arguing about hypothetical stuff.

Unfortunately, I don't know how to make Confluence search in pages only by default. You can use the detailed search settings on the serach results page.

#7

"def" is not a keyword in Kotlin.

Thanks for confirming.

Unfortunately, I don't know how to make Confluence search in pages only by default. You can use the detailed search settings on the serach results page.

It’s a pity that Confluence won’t even weigh the results so that hits on pages appear before hits on comments.