Post

One of Kotlin’s wildest features: you can define control flow altering functions using lambda expressions with a special syntax. Thanks to aggressive inlining it’s basically free. #kotlin

28 reposts489 likes

In the middle of a tight inner loop especially if needed in two or more different loops. It saves the overhead of saving the stack but isn't entirely free as it increases the memory footprint

It is not free. It has an cost in form of limitations on advanced modern tools operating on the code other than compilers: program proofs, advanced static analysis, IDEs, debuggers. These "macro" features were present in old languages (lisp, C, ...) but abandoned for a reason.

It is just a light syntactic sugar over passing a function to function. It just happens to compose well with inlining support in kotlin. Far less powerful than macros, and all kotlin tooling perfectly supports it.

Ah yes you are right. Tools working at the kotlin source level seem to work fine. This leaves inconveniences only for runtime tools (debuggers, coverage, profiling), but nearly never visible in practice. My bad. Too much C and Lisp in my young years :-)

I used to write ruby for a living at some point. But for me I don't think syntactic sugar can possibly make anything more readable. If we understand readable as ability to quickly see what exactly is going on. Not just "yeah this is text".

Huh, neat. That block syntax for lambda/closure expressions reminds me of Swift (which, to be clear, I absolutely detest as a language but I do think it has some good parts)

(Or, for that matter, maybe the function supports some kind of currying in general and blocks can be implicitly converted to lambdas -- there's not enough context in this code snippet to infer exactly how it works.)

One of the main reasons that Java was successful, back in the day, was that (unlike C++) it did not allow exactly this: the creation of new languages. When you got code back from some offshore contractor, there was a fighting chance you could at least read it