top | item 34272022

(no title)

dima_vm | 3 years ago

Absolutely damned is combination of Kotlin's several features:

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.

discuss

order

plucas|3 years ago

What exactly do you mean by "damned" here?

sirsinsalot|3 years ago

I too am confused by the unique use of this word

brabel|3 years ago

I am assuming the author is using it as one would use "wicked" (with a positive conotation)?

brabel|3 years ago

All of these were copied from Groovy, in case you didn't know it.

pas|3 years ago

It reminds me of Scala too, but I have no idea which started when. And of course both could have come up with this fairly independently. (After all it is the norm in functional style.)

zorr|3 years ago

Kotlin's features are amazing for designing DSLs but I think your code sample uses more than just the 3 points you mentioned. Specifically the `call.respondText(..)` part. I assume that in this example the second argument to the `get` function is actually a "lambda with receiver", which means that the lambda executes with another object as the receiver (and that object is bound to "this" inside the lambda block), which makes the `call` object available.

still_grokking|3 years ago

Isn't it cool that you can't know what the code is actually by just looking at it? /s

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.

still_grokking|3 years ago

> 1. Lambda functions can be defined with {}.

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… ;-)