- 34
- 206 509
Dave Leeds
เข้าร่วมเมื่อ 18 ก.ย. 2022
Anonymous Functions Aren't Lambdas
Rarely spotted in the wild, Kotlin's anonymous functions blend some features of named functions with some features of lambdas. In this video, we'll check out what anonymous functions are and when we might want to use them.
✨ New to Kotlin? Start your journey here: typealias.com/start/
... or pick up the new Leanpub Edition for offline access and more! book.typealias.com/
🚀 Get the inside scoop on everything I'm working on - join my new email newsletter!
Sign up here: newsletter.typealias.com/
🛍️ Check out the newly-launched TypeAlias Shop for some fun merch!
shop.typealias.com
📚 Chapters
00:00 Anonymous Function Basics
02:23 Return Keyword
05:23 Explicit Types
07:24 Wrap-up
✨ New to Kotlin? Start your journey here: typealias.com/start/
... or pick up the new Leanpub Edition for offline access and more! book.typealias.com/
🚀 Get the inside scoop on everything I'm working on - join my new email newsletter!
Sign up here: newsletter.typealias.com/
🛍️ Check out the newly-launched TypeAlias Shop for some fun merch!
shop.typealias.com
📚 Chapters
00:00 Anonymous Function Basics
02:23 Return Keyword
05:23 Explicit Types
07:24 Wrap-up
มุมมอง: 2 573
วีดีโอ
Fun with Function Types in Kotlin
มุมมอง 3.3K21 ชั่วโมงที่ผ่านมา
Let's look at the different kinds of function types in Kotlin, and see how we can call regular functions - and even property setters - as if they were extension functions! In this video, we'll examine standard function types, extension function types, bound references, and property references - and we'll pick up some fun tricks along the way! ✨ New to Kotlin? Start your journey here: typealias....
Subtyping Composables - TypeAlias Show Clip
มุมมอง 2.8K14 วันที่ผ่านมา
"If you want to create a design system in Compose where we use a slot API that doesn't allow any composable but only allows specific ones, how would you do that?" Here's one idea that worked for me, and I'd love to hear your thoughts about it! ✨ New to Kotlin? Start your journey here: typealias.com/start/ ... or pick up the new Leanpub Edition for offline access and more! book.typealias.com/ 🚀 ...
Applying the Builder Pattern in Kotlin
มุมมอง 4.3Kหลายเดือนก่อน
The Builder design pattern enables us to construct complex objects in multiple steps. But how much of it is relevant for a modern programming language like Kotlin? In this video, we'll see a traditional version of the Builder Pattern, and then we'll see how we can improve upon it with Kotlin's language and library features. ✨ New to Kotlin? Start your journey here: typealias.com/start/ ... or p...
Applying the Factory Method and Abstract Factory Patterns in Kotlin
มุมมอง 4.9Kหลายเดือนก่อน
Make object creation more flexible with the Factory Method and Abstract Factory design patterns! Today, we'll look at code that could benefit from these patterns, we'll apply a traditional version of them from the classic Gang of Four Design Patterns book, and then we'll see how we can modernize them with Kotlin's language features. ✨ New to Kotlin? Start your journey here: typealias.com/start/...
Applying the State Pattern in Kotlin
มุมมอง 7K2 หลายเดือนก่อน
When our objects behave differently in different states, then it can be helpful to apply the State Pattern. Today, we'll look at code that could benefit from the State Pattern, we'll apply a traditional State Pattern from the classic Gang of Four Design Patterns book, and then we'll see how we can modernize it and reduce boilerplate by using some of Kotlin's language features. ✨ New to Kotlin? ...
Applying the Decorator Pattern in Kotlin
มุมมอง 6K3 หลายเดือนก่อน
The Decorator Pattern allows us to augment the behavior of an object - without the object itself knowing about it! In this video, we'll start with code that could benefit from this pattern. Then we'll apply the Decorator Pattern using the classic approach as outlined in the original Gang of Four book. Finally, we'll modernize the pattern using some of Kotlin's language features. ✨ New to Kotlin...
Applying the Strategy Pattern in Kotlin
มุมมอง 8K3 หลายเดือนก่อน
The strategy pattern is one of my favorites from the original Design Patterns book. In this video, we'll see why the strategy pattern is helpful, we'll apply the pattern in Kotlin, and then we'll see how we can use some of Kotlin's language features to put a more modern spin on it. ✨ New to Kotlin? Start your journey here: typealias.com/start/ ... or pick up the new Leanpub Edition for offline ...
Coroutines: Concurrency in Kotlin
มุมมอง 17K3 หลายเดือนก่อน
In this presentation, I give an overview of coroutines in Kotlin, including the basics of coroutines, how they can be used for concurrency and parallelism, and how structured concurrency helps ensure coroutines are cleaned up in response to cancellations and exceptions. The content for this presentation is largely based on chapter 20 of Kotlin: An Illustrated Guide, which you can read online he...
Kotlin Code Reuse: Composing like you're Inheriting
มุมมอง 11K4 หลายเดือนก่อน
The classic Design Patterns book introduced the principle of favoring composition over inheritance. In this video, we'll explore what the authors meant by this principle, we'll consider the characteristics that are affected by it, and we'll look at an underrated Kotlin feature that can change our perspective on it! ✨ New to Kotlin? Start your journey here: typealias.com/start/ ... or pick up th...
7 Features in Kotlin's Standard Library that You Might Have Overlooked
มุมมอง 7K4 หลายเดือนก่อน
Are you still using the Java API when you don't have to? Are you rewriting functions that are already in the standard library? In this video, I cover seven features in Kotlin's standard library that you might have missed or forgotten about. 🕙 What days and times work best for you for a livestream? forms.office.com/r/9vpuJv0PMq ✨ New to Kotlin? Start your journey here: typealias.com/start/ ... o...
Reified Type Parameters
มุมมอง 4.4K5 หลายเดือนก่อน
Learn what happens to type arguments when our Kotlin code is compiled, and how we can step around certain type erasure problems with reified type parameters. 📽️ Inline, Crossinline and Noinline video: th-cam.com/video/T9sAlxqYFYc/w-d-xo.html ✨ New to Kotlin? Start your journey here: typealias.com/start/ ... or pick up the new Leanpub Edition for offline access and more! book.typealias.com/ 🚀 Ge...
Inline Functions: inline, crossinline, and noinline
มุมมอง 7K7 หลายเดือนก่อน
Inline Functions: inline, crossinline, and noinline
Type Projections... and why they work!
มุมมอง 4K8 หลายเดือนก่อน
Type Projections... and why they work!
Easy SharedPreferences with Delegated Properties in Kotlin
มุมมอง 9K2 ปีที่แล้ว
Easy SharedPreferences with Delegated Properties in Kotlin
5 Fun Ways to Use Extension Functions in Kotlin
มุมมอง 13K2 ปีที่แล้ว
5 Fun Ways to Use Extension Functions in Kotlin
Peek Behind the Scenes of TypeAlias.com - Dave Leeds on Kotlin
มุมมอง 1.4K2 ปีที่แล้ว
Peek Behind the Scenes of TypeAlias.com - Dave Leeds on Kotlin
Thanks Dave, these videos are so good. I have been using Kotlin in old Java ways, not really using the language features.
Hey, thanks so much Navdeep! Yes, it's natural to bring along the mental model from previous languages we've worked with. Takes a little while to adjust to the new language features.
Thanks Dave! Your Kotlin content is Gold.
Thanks so much, GB! I'm glad you're enjoying it! 🎉
All very interesting, but a double-edged sword. The usefulnes is limited and comments here prove that it is widely unknown. I understand that this may be a product out of genericity and completeness, but it in my book a language is a means of communication between developers. What I write, another needs to understand. If they have to ponder on a piece of code due rarely used idoms, it slows them down and they might miss crucial implications (here: return value logic.) Don't get me wrong, though, it sure is an interesting corner of the language to explore.
Yes, always a good idea to be mindful of the experience and perspectives of other developers on the team (both current and future). If your teammates aren't familiar with an idiom, one option is to avoid it; another is to introduce them to it and see if it's one that makes sense to them and they'd like to adopt. But in any case, development a usually a team effort, and it pays to write code that'll be understood by everyone on the team.
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.
Hey Bruno - wow, that's a fantastic, simple way to remember it! 🎉 Thanks so much for sharing it
Best Kotlin content! Thanks Dave
Hey, thanks GB! I appreciate that!
The state class implementations should have context. In this case User class as parameter. And you shouldn't call print in state class, but rather call user class methods. This is really misleading. Print is just inner implementation of user class. Thanks anyway for sharing your experience
I feel like the android-maps-compose library does a better job here. It keeps the slot APIs at it is, but introduces a new annotation (called @GoogleMapComposable) that restricts you to only use this library's composables. For example: ```kotlin @Composable fun MapPage() { GoogleMap { // I can use this since it's marked with @GoogleMapComposable AdvancedMarker() // I cannot use this since it's not marked with @GoogleMapComposable Box() } } ``` Using classes to depict views in my opinion breaks Compose's functional approach of depicting a view-tree and goes more into the old XML View's class based domain.
This is a little bit advanced for me
After watching your video, I feel my code was totally shit
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.
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.
@@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.
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."
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.
I've been writing kotlin since 2018 and never came across this syntax till today :O
Yeah, it's not one that you see out there often!
Why does your every video makes me feel that I know nothing about Kotlin 😂
Kotlin is deep and well designed, imho, so you dont need to go into every corner to get the benefits.
Yes, you can definitely be productive with the more primary features! (Those corners sure are fun to explore, though!)
I thought I knew everything about Kotlin syntax, but every new video of yours shows me something new. This language is awesome!
Hey, glad I could introduce you to something new, Vemu! Yeah, I really love working in Kotlin!
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") }
@@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
Good point
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
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" }
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! 🎉
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.
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.
TIL... Continue with the awesome content
This video is incredible ! I love your way of explaining things, we really feel you have a passion for that. I wanted to know what do you think about uniform function call from some languages like Nim ?
Hey, thanks so much, Fabrice! I appreciate your kind words! Although I haven't got much experience with languages that support uniform function call syntax, in concept I like the convenience of it. Of course, it's not always natural for the first parameter to be used as a receiver - e.g., `createUser(firstName, lastName, age)` would be confusing as `firstName.createUser(lastName, age)` - so regardless of whether the developer has to opt in to the "method" syntax or whether the language gives it to you out of the box, developers still need to use discretion. Have you got thoughts about it yourself?
@typealias thanks for your feedback ! I thought the same ! I implemented it for a language I am building (still far from finished because of the complexity of different features I joined together ^^') It's quite convenient since you can switch between a normal call (f(x)), it's "method call" (x.f()), it's pipe call (x |> f()) or his arrays versions (f#([x]), [x].f#(), [x] |> f#()). But when it's too convient, that can mean there is a potential problem. That's why I wanted to know how kotlin functions works to see if there is a benefit (security, efficiency, etc.) in the way it does things. Actually I don't really know🤔
Hey Dave! I just wanted to say thank you for your wonderful book and videos. It really helps!
Thank you for your kind words, Aleksandr! I'm so glad you've been enjoying them.
Great video! Do you have any resources or plans on using Socket Servers within kotlin? After learning all these things i’m looking into sockets for sending “commands” between my projects to act upon!
Hey Stefano! I've used websockets with Ktor ( ktor.io/docs/server-websockets.html ) for that kind of thing, but pretty sure I'll end up updating those projects to use kotlinx.rpc ( github.com/Kotlin/kotlinx-rpc ) once that project is a little farther along. I showcased some of kotlinx.rpc in a livestream a few months back. You can jump to that segment here: th-cam.com/users/liveS_JKbmN_A8o?feature=shared&t=389
Your videos are crazy, you are cazy! In a good way!
10:34 how did this convert happen?
3:47 What the heck happened? Can you explain how to do it
This is all cool and clever but if I ever saw this in a PR, I was kick it back.
Certainly a feature to be used judiciously
Amazing video, thanks for sharing :) i have a question for the last example, using Lamdas with receivers u can pretty do everything extension function do ? If so what is the difference between them ?
Hey, thanks so much! The differences between a lambda with a receiver and an extension function are essentially the same as the differences between a regular lambda and a regular function. The biggest differences are that a lambda is an expression, the `return` works differently in a lambda than a function, and lambdas require type inference to determine a return type and receiver type. You'll see some of those differences demonstrated in next week's video.
@@typealias Thank u very much :), can't wait for next week vid
why not you become kotlin team lead, senior manage CEO , CTO and all other position at jet brains
Hey, that's kind of you to say! That'd be a lot of responsibility, though... so I'm going to stick to making videos! 🙂
but you will take kotlin to 1# loved lanuage
really interesting one. Some things are really ez in kotlin, but some things are blewing my mind still to this day. Thank you Dave. Really Gucci stuff.
Thanks so much! So many fun things to discover in Kotlin!
Great video as usual! Looking back, I used the implicit switching between extension and non-extension functions many times before without even realizing it! Also didn't know you could take references to property setters/getters, that's pretty neat!
Thanks!
Hey Dale! That's very kind of you. Thank you so much! 🎉
I just realized that I don't know shit about Kotlin functions...
Damn that’s neat! My mind was racing with practical applications to try out
Let me know what ideas you come up with!
I like that you use one line property delegation by: val length get() = text.length I switched to an even shorter form: val length by text::length Advantage is that it is still one liner for var properties.
Ooooh! I like that! I'm going to start using that! 😁
@@typealias I dont know if there is a difference in bytecode. But i believe in the power of the incredible compiler superman architects.
😂
Oh God that's so gorgeous!
Great stuff, as usual! Thank you so much, Dave!
Thanks so much, Arthur!
thumb up
Every time I think I will know everything in the video, you surprise me! I've always wanted to know if using these types of references are we doing any kind of reflection or not, since the symbol is the same
If you don't explicitly specify a type (e.g., `val x = ::countWords`), then a function reference will be one of the `KFunctionN` types, which gives you access to reflection properties and functions. But unless you've got the `kotlin-reflect` library added to your project, you'll really only have access to the `name`. Stay tuned - I've got a video queued up that explains all of this further... won't be the next video, but probably the one after that.
Thank you
Wow thank you.
This is fun 😀
Just when I thought I knew everything about function types
Lots of neat little tricks just waiting to be exploited! 🙂
you're good!!! 🤣
Thanks, from Vietnam😘
Great explanation ❤. You deserve more subscribers
Hey, I appreciate that, Vlad!
37:58 I've actually set up my keyboard such that IJKL are my arrow keys system-wide when pressed with the right-side command key (would be Alt Gr/right-side alt on Windows). I got used to it really quickly and now I feel noticeably faster navigating anywhere on my computer.
Wow, system-wide? That's really cool! Is that possible to do with just the baked-in MacOS tools, or are you using any particular software to make that happen?
best explanation
I don’t see any benefit here, slot pattern is make it more open but here we are restricted to only ToolbarButton. Why not just use toolbarText in Toolbar composable?
Nice. I hate documentations. Thats another way to rip off documentation and replace it by self explaining coded rules.
Didnt know that, Kotlin is awesome. IntelliJ is awesome. Jetbrains is awesome. Your videos are awesome.
There is an excellent book called "Secure by Design" I believe this approach reduces the number of bugs during development.
Hi Dave👋 Thank you for your efforts, thank you for your efforts