Naming: Continuations or Closures


#1

Hey!

From what I see, Java lambdas are called “function literals” in Kotlin [1]. There is a recent Reddit discussion about the Java and Kotlin syntax for them. In the discussion [2] it appeared to me, that the Kotlin syntax for them is better suited for builders and Groovy-style closures, and both of them are used for important features in some high-profile Kotlin projects on GitHub.

I wonder if we could find a suitable name for the Groovy-style closures or continuations. There are some paragraphs that describe them in [1], but no explicit name is given. Maybe we could find some good use-cases of them, and add examples to the Idioms chapter [3] in the Kotlin reference docs.

[1] http://kotlinlang.org/docs/reference/lambdas.html#higher-order-functions
[2] https://www.reddit.com/r/Kotlin/comments/3jx8nd/a_aa_or_a_aa_is_kotlins_syntax_for_lambdas_better/cutibsc
[3] http://kotlinlang.org/docs/reference/idioms.html


#2

The Kotlin grammar names them "callSuffix" [1].

>Note that if a function takes another function as the last parameter, the function literal argument can be passed outside the parenthesized argument list. See the grammar for callSuffix.

[1] http://kotlinlang.org/docs/reference/grammar.html#callSuffix

IDEA has an intention that just titles “Move lambda argument out of parentheses” and mentions no explicit name for them.


#3

A few confusions seem to have happened here :)

First off, in Kotlin we are migrating our terminology to use “lambda” instead of “function literal”. (Plus, we now have anonymous function expressions: “fun () {}”)

Then, the lambdas passed outside of the parentheses (the syntax we borrowed from Groovy) are by no means continuations. I mean, they may be used as such, but neither their syntax nor semantics are designed to facilitate or promote that particular type of usage.

“callSuffix” in the current grammar is anything after the function name: type parameters in angle brackets, parenthesized value parameters and the lambda in question.

It would be nice to find a term for the syntax of passing lambdas outside of parentheses, one option could be just “trailing lambda” for example. But I can’t say I see a really good term right now.


#4

Thanks for the clarification!

I think it would be worthwhile to have a glossary for Kotlin-specific terminology with short examples. It took me a while to skim through the reference guide to find the name for the T.() -> Unit construct used in T.run(f: T.() -> R): R and T.apply(f: T.() -> Unit): T. Seems this must be a “parameter type for an extension function literal” or something.

Also: As I understand a extension function literal is primarily used to be able to use this (or directly use properties and methods of this) instead of it, which can be more or less be seen in the difference between let() and apply() in [1].

[1] https://github.com/JetBrains/kotlin/blob/build-0.13.1514/libraries/stdlib/src/kotlin/util/Standard.kt#L29-L52