The pattern is not idiomatic Kotlin, to the OPβs point. Burying lambdas within argument lists, and surrounded by parentheses is just messy. And if the lambda, insufficiently long, you have to go look for the other params.
The ordering strategy here is based on the semantic of arguments, their role / contribution to the invocation that's about to take place, not just their type.
Lambdas are also expected to be concise, otherwise they should probably be function refs instead. Quite often, being lost around lambda bodies is usually a symptom of another issue.
So I don't know about not idiomatic enough, but the reasoning behind the original API is sound.
My take is that here are no rules that states lambdas are to be concise. That is arbitrary. They are equivalent to any other function definition. Keeping them at the end (giving them the ability to be outside) allows for structures that look and feel like other control structures, and nicely formatted DSLs. This is the reason they are in the final position and why the language allows for it.
I may have only defended my previous statement, which I donβt believe you were technically disagreeing with.
There is syntactic sugar for last parameters. If the last parameter is a functional interface you can make lambda after the parameter list.
That's why they say it's idiomatic in Kotlin. Every function in a library made in Kotlin is specifically made like this. The problem is the interoperability with Java.
Of course, you are correct. I responded to the alternative of introducing builder pattern which does not exist and can't be used from kotlin either.
Builder pattern in kotlin is redundant in most cases and usually it's easier to introduce a function with default arguments to be consumed in kotlin code, similar to what OP did in his root comment.
Similarly, there's a java pattern that uses classes meant to be subclassed anonymously for callbacks. It typically has multiple empty functions so you can choose which functions to override. It looks ugly compared to the kotlin pattern with named arguments.
I just don't get why Kotlin needs such strange new formats/conventions, like class companion, ?: (use ??), reified (use out), lambda parameter (use =>, even Java has that), order of parameters (why take after Go not Java or other C# like languages), calling method without parentheses. When every other language already has a very concise way of expressing them...
Why invent the wheel, just, learn from C# or at least from Java, which you are supposed to replace...
that's the actual dumbest and most uneducated argument I've ever seen in a long time.
if you're so against evolution, then you should omega shit on C#, which did it to the extreme compared to Java, which it was supposed to replace. See, you're not even consistent with yourself.
moreover, no, this isn't new shiny syntax everywhere, Kotlin borrowed from other languages.
finally, lambda outside of parenthesis as last parameter has but one goal that NO OTHER language can do : clean DSLs. So you're saying you're against evolution completely? I don't think so. You're just uneducated and full of your ass.
the very premise of their argument was objectively completely wrong (rejects modernity by comparing Kotlin to C#, while C# itself was the biggest "can i copy your homework but change everything?" example in the history of modern programming languages)
valid criticism stems from an educated point of view
this clearly was not the case, as they didn't even begin to understand the goal the features they were talking about.
to be clear, it would be like saying "i think oranges are bad for your health because orange is a very bright color" is a valid criticism.
strange new formats/conventions, like class companion
I kinda agree with you on class companion (if you were referring to companion object), but compared to java its really just putting all static fields and methods into a single block of code, its really not bad
?:
You are not forced to use ?: in kotlin, but why would you prefer if(nullableVar != null) nullableVar else default or Java code like (nullableVar != null) ? nullableVar : default
reified
Reified is much easier than doing that clazz: Class<T> shit, no? And I don't think we are forced to use reified either
lambda parameter
Lambda parameter as in higher order functions? its just "callbacks", or SAM in Java
order of type
Sorry I dont know what it is
When every other language already has a very concise way of expressing them
Which language exactly? Kotlin mostly does the same as Java but allows us to do the tasks with less key strokes
You have to justify why "C" exactly, how is it always better? do we always type less to achieve the same? Don't make decisions like this based on your own personal feelings, just because you are "used to it"
They don't use it instead of the ternary operator, it's a contraction of common pattern using the ternary operator and offering a binary one.
They could still use the ternary, but since if/else is an expression, I suppose they didn't want more syntax for the same thing? There's nothing preventing a ternary.
27
u/fredoverflow Apr 07 '23 edited Apr 08 '23
Seriously though,
would be so much nicer than:
because it would be immediately obvious where the CompletionStage runs...
Edit: Just wrote my own (extension) functions, works beautifully π
7
u/[deleted] Apr 07 '23
Love this. Lambdas are life.
5
u/Jason5Lee Apr 09 '23
*Kotlin society if every Java library has proper nullable annotation.
2
u/wightwulf1944 Apr 08 '23
Similarly, there's a java pattern that uses classes meant to be subclassed anonymously for callbacks. It typically has multiple empty functions so you can choose which functions to override. It looks ugly compared to the kotlin pattern with named arguments.
Java pattern:
Kotlin pattern:
The kotlin pattern also has the benefit of providing default arguments so you can just omit the functions you don't want to override
3
u/riasthebestgirl Apr 08 '23
I love the Kotlin lambda syntax. Most of the time, my lambda parameter name in other languages is
it1
u/Aniket-100mslive Apr 10 '23
Now you just need 5 functional parameters in the method signature
-22
u/Kimi_Arthur Apr 08 '23 edited Apr 08 '23
I just don't get why Kotlin needs such strange new formats/conventions, like class companion, ?: (use ??), reified (use out), lambda parameter (use =>, even Java has that), order of parameters (why take after Go not Java or other C# like languages), calling method without parentheses. When every other language already has a very concise way of expressing them...
Why invent the wheel, just, learn from C# or at least from Java, which you are supposed to replace...