by dima_vm · 3y ago · 0 comments
1. Lambda functions can be defined with `{}`.
2.`foo(bar, somefunc)` is the same as `foo(bar) somefunc`. In other words, if the last parameter is a function, it can be provided AFTER closing parenthesis.
3. Interfaces that require only one method can be implemented on-side with a lambda function (i.e. `{}` syntax for no-param function).
Combined those three features, the code may look like that:
routing {
static("/statics") {
files("css")
}
get("/foo") {
call.respondText("Hello world!")
}
}
So you can make a config-looking file which is just pure Kotlin, with static type checking, autocomplete, suggestions, "this" etc.It's so damned, I'm surprised author didn't mention it.
Not that I was completely innocent of this at that age.
/Rant.
Kotlin was started even as just a poor Scala clone. (Because JetBrains didn't manage to get a working Scala plugin for their IDE, so they thought it would be simpler to create their own "simpler" version of the language).
Kotlin's scope injection is one of the most terrible "features" ever invented. It's dynamic scoping on steroids!
But dynamic scoping was long ago deemed a horrible bug and never ever made it again into any new language.
Directly "stolen" form Scala.
> 2.foo(bar, somefunc) is the same as foo(bar) somefunc. In other words, if the last parameter is a function, it can be provided AFTER closing parenthesis.
Just a irregular syntax quirk that tries to get around the fact that Kotlin does not support multiple parameter lists, like the language where most Kotlin features come form, Scala.
> 3. Interfaces that require only one method can be implemented on-side with a lambda function (i.e. {} syntax for no-param function).
That doesn't have anything to do with Kotlin. That's Javas SAM (Single Abstract Method) feature.
> I'm surprised author didn't mention it.
The author seems not to know any Scala. Otherwise the lists would show mostly only Scala features… ;-)