Anonymous Functions Aren't Lambdas

  Рет қаралды 2,446

Dave Leeds

Dave Leeds

Күн бұрын

Пікірлер: 23
@markopi64
@markopi64 Күн бұрын
Anonymous functions work great with currying: fun greet(greeting: String) = fun (name: String) { println("$greeting, $name") } greet("Hello")("Dave") -> Hello, Dave val goodbye = greet("Goodbye") goodbye("Dave") -> Goodbye, Dave Looks more natural than with lambdas: fun greet(greeting: String) = { name: String -> println("$greeting, $name") }
@snowe..
@snowe.. Күн бұрын
@@markopi64 wow. I was trying to do currying just the other day and you’re right, lambdas look terrible. Anonymous functions definitely look more readable
@teenriot-de
@teenriot-de Күн бұрын
Good point
@cktyt
@cktyt Күн бұрын
Wow it really is better; it becomes really clear when we put the alternatives together: fun greet1(greeting: String) = fun(name: String) = println("$greeting, $name") val greet2 = { greeting: String -> { name: String -> println("$greeting, $name") } } fun greet3(greeting: String) = { name: String -> println("$greeting, $name") } `greet1` is just so much easier to read compared to the other two
@cktyt
@cktyt Күн бұрын
Though it is not as elegant compared to lambdas, when we have to return something from the function. The "implicit return" feature of lambdas is nice to have in these cases: fun greet1(greeting: String) = fun(name: String): String { return "$greeting, $name" } val greet2 = { greeting: String -> { name: String -> "$greeting, $name" } } fun greet3(greeting: String) = { name: String -> "$greeting, $name" }
@typealias
@typealias 17 сағат бұрын
Hey, that's fantastic! Yeah, definitely looks cleaner than using lambdas - especially when you'd have to nest them. And the lambda implementation above could be confusing - when I've done that in the past, I've sometimes had to clarify to other developers that it's an expression body with a lambda, rather than a function with a block body. Thanks for sharing, Marko! 🎉
@shreyashkore2076
@shreyashkore2076 Күн бұрын
Why does your every video makes me feel that I know nothing about Kotlin 😂
@ChrisAthanas
@ChrisAthanas Күн бұрын
Kotlin is deep and well designed, imho, so you dont need to go into every corner to get the benefits.
@typealias
@typealias 17 сағат бұрын
Yes, you can definitely be productive with the more primary features! (Those corners sure are fun to explore, though!)
@Vemu
@Vemu Күн бұрын
I thought I knew everything about Kotlin syntax, but every new video of yours shows me something new. This language is awesome!
@typealias
@typealias 17 сағат бұрын
Hey, glad I could introduce you to something new, Vemu! Yeah, I really love working in Kotlin!
@brunojcm
@brunojcm 3 сағат бұрын
There's a nice rule of thumb for `return` statements for that case with the inline lambda returning the `main` function: a `return` statement always returns the closest `fun`; In the case of the return inside the inlined lambda call, that is the `main` function, and for the anonymous function, it has its own `fun` so it returns there.
@bjmi_de
@bjmi_de Күн бұрын
A great video again. 10.3.3 Anonymous functions: Local returns by default (Kotlin in action 2nd ed) "A. f. mainly shorten code that has a lot of early return statements, which would need to be labeled when using the lambda syntax." and "despite the fact that an a. f. looks similar to a regular function declaration, it's another syntactic form of a lambda expression."
@typealias
@typealias 17 сағат бұрын
Thanks Björn! Yeah, I agree with the authors insofar as anonymous functions are also function expressions. But due to the differences in the semantics of the returns, I find it more precise to regard lambdas and anonymous functions as two different ways to write _function literals_, rather than regarding anonymous functions as simply a different syntax for a lambda. Of course, the mental model that's best for each developer is the one that's most helpful to him or her, so if "like a lambda" is helpful to some developers, I wouldn't argue about it.
@gb_kash
@gb_kash Күн бұрын
Best Kotlin content! Thanks Dave
@typealias
@typealias 17 сағат бұрын
Hey, thanks GB! I appreciate that!
@snowe..
@snowe.. Күн бұрын
Great video, I hope someone comments with a use case that you didn't think of, but I'm guessing it's just a case of syntactic sugar. So many people used the lambda syntax that it got workarounds to make it work like an anonymous function, rather than leaving them distinct.
@typealias
@typealias Күн бұрын
Thanks, Tyler! Yes, I'll be interested to see what use cases are brought up. Specifically wondering whether there are any cases where the compiler has trouble inferring the types of a lambda but where an anonymous function would clear it up. I haven't been able to produce any myself, though.
@IsuruKusumal
@IsuruKusumal Күн бұрын
I've been writing kotlin since 2018 and never came across this syntax till today :O
@typealias
@typealias 17 сағат бұрын
Yeah, it's not one that you see out there often!
@teenriot-de
@teenriot-de Күн бұрын
not true but: - java is academic driven -> programmers are students -> dont trust students, you have to put them under tutelage - scala is community driven -> programmers are teenagers when parents are away over weekend -> house get destoryed but in a creative way - kotlin is company driven -> programmers are costumers that are served and gerrymandered (in a good way) -> if you care about customers, especially in mid life crisis, you dont show them everything you have, otherwise you lose them when they become teenagers without parents ----------- i like that kotlin released me from java imprisonment, but i fear that when you are free, you are free to make more mistakes. Strong antetypes are needed to replace java prohibition by natural kotlin authority in freedom. In this video are a lot of example where authority is needed to make people not to use them without really good reason. When java is right at one thing then it is worship of holy idiomaticism. The big scala mistake was already done, no need to repeat it. But still good to know all those things, there are valid corner cases for every feature.
@typealias
@typealias 17 сағат бұрын
Hey, thanks for commenting! I hadn't thought about Java/Scala/Kotlin that way before, but I see what you're getting at. Yeah, you might have noticed that I don't often recommend what developers "should do" because it depends on the context. Anonymous functions are an interesting and often-overlooked feature that - like all other language features - should be used with discretion.
@teenriot-de
@teenriot-de 16 сағат бұрын
@@typealias People should go in playground mode like you do, but don't forget to switch back to production mode, where you write code for others, even when you write it just for yourself. Take the best and skip the rest.
Kotlin Code Reuse: Composing like you're Inheriting
14:42
Dave Leeds
Рет қаралды 10 М.
Inline Functions: inline, crossinline, and noinline
11:59
Dave Leeds
Рет қаралды 7 М.
ТЮРЕМЩИК В БОКСЕ! #shorts
00:58
HARD_MMA
Рет қаралды 2,5 МЛН
Coroutines: Concurrency in Kotlin
30:22
Dave Leeds
Рет қаралды 16 М.
Fun with Function Types in Kotlin
11:35
Dave Leeds
Рет қаралды 3,2 М.
Three Simple Rules for Subtypes
13:52
Dave Leeds
Рет қаралды 2,8 М.
Applying the State Pattern in Kotlin
12:55
Dave Leeds
Рет қаралды 7 М.
Every Kind of Class in Kotlin
10:44
Dave Leeds
Рет қаралды 14 М.
Reified Type Parameters
10:15
Dave Leeds
Рет қаралды 4,3 М.
The Essence of Coroutines
8:10
Dave Leeds
Рет қаралды 12 М.