5 Fun Ways to Use Extension Functions in Kotlin

  Рет қаралды 13,821

Dave Leeds

Dave Leeds

Күн бұрын

Пікірлер: 94
@Alchemist10241
@Alchemist10241 10 ай бұрын
10:54 - this operator extension function is very useful, by using this extension function I saved a lot of time when I was working on a project that had a lot of Maps in it
@typealias
@typealias 10 ай бұрын
That's great! I never had a need to use that one in a real app, but I'm glad to know it worked well for your project!
@TimSchraepen
@TimSchraepen 2 жыл бұрын
Nice overview. I can see some of them being useful in custom DSL’s.
@typealias
@typealias 2 жыл бұрын
Thanks Tim! Yes, I agree - a custom DSL would be a great context for some of them!
@martingaens2073
@martingaens2073 2 жыл бұрын
Haha yeah I can confirm this is indeed a great way to teach about these funky operators. Kotlin has quite some flexibility with these things. Great vid :)
@typealias
@typealias 2 жыл бұрын
Thanks, Martin - Yes, those operator conventions allow for some pretty neat possibilities!
@stephanbranczyk8306
@stephanbranczyk8306 2 жыл бұрын
Wow! This is a gem of channel. I'm glad it was recommended to me
@typealias
@typealias 2 жыл бұрын
Thanks, Stephan! I'm still figuring out what kind of videos are most helpful for everyone, so I'm glad to hear that you're happy with the channel so far!
@abdushakoor0099
@abdushakoor0099 2 ай бұрын
really like the #4 which can come very handy when you're dealing with network and local database models
@victoryghor9009
@victoryghor9009 Жыл бұрын
I am studying kotlin, and really love your material it's soo easy to understand with this simples examples, please continue your channel.
@typealias
@typealias Жыл бұрын
That's great! Thanks so much for your kind words, Victor! I'll keep up the videos!
@shotoneko
@shotoneko 3 ай бұрын
Your book is great, completely recommended for anyone who wants to learn quickly and clearly, from the basics.
@typealias
@typealias 3 ай бұрын
Hey, thanks so much, Renato! I'm really glad to hear that you've enjoyed the book!
@LifeLessonsLearned8587
@LifeLessonsLearned8587 2 жыл бұрын
Absolutely amazing!! This channel is gem!!
@typealias
@typealias 2 жыл бұрын
Thanks so much, Vivek! 🙂
@theblueplanet3576
@theblueplanet3576 11 ай бұрын
Thanks for sharing this. The last one with combination of infix and extension function was crazy but useful 😁
@typealias
@typealias 11 ай бұрын
Thanks! Haha, yeah, that one is a little crazy, and it might leave some fellow developers scratching their heads. But it sure is fun to explore all the crazy possibilities! 😁
@ChristianMielke
@ChristianMielke Ай бұрын
this Video is so 🤯I'm a professional Java Developer, wich sitching currently to Kotlin, this is my 3th Video from you, and i love it. This is Nerd P**n
@typealias
@typealias Ай бұрын
Hey, thanks so much, Christian! 😁 Glad you're enjoying the videos!
@brianlcooley
@brianlcooley 9 ай бұрын
Just discovered your channel today. Great stuff, Dave!
@typealias
@typealias 9 ай бұрын
Hey, thanks so much, Brian! Glad you're here!
@smreha
@smreha 6 ай бұрын
I was blessed by the YT recommendation algorithm as 1 of your videos showed up. It was an instant sub which is very rare for me. Keep up the good work 👍
@typealias
@typealias 6 ай бұрын
Hey, I'm thrilled to hear you're enjoying the channel! Glad to have you here! 🎉
@blaupunkt1221
@blaupunkt1221 6 ай бұрын
That was really interesting and easy to understand. Thank you for showing this!
@GrigoryTukmachev-y5u
@GrigoryTukmachev-y5u Жыл бұрын
Dave, thank you for your work and all your videos. They are very professional and positive. I found them easy to watch and consume information... at the same, they provide a lot of "concentrated" information about their topic. Thanks again, and, please, keep doing more videos! P/S I've recommended your channel to my colleagues.
@typealias
@typealias Жыл бұрын
Thanks so much, Grigory! Glad to hear that the format seems to be working well. I'll keep at it! 🙂
@rikaryu5411
@rikaryu5411 2 жыл бұрын
Thank you very much! Such great videos. They really stand out from the rest of the similar content because of the way you explain everything - very clear and concise way of putting everything, helpful and comprehensive examples, nice pace and non-trivial topics and also fun! I guess you might even pick the topics that are more trivial and still your videos will be just as good. Please keep doing it :)
@rikaryu5411
@rikaryu5411 2 жыл бұрын
Also that would be awesome to watch something about Coroutines on your channel!
@typealias
@typealias 2 жыл бұрын
Thanks so much for the feedback, Rika - I'm glad to hear that these videos have been helpful and entertaining for you! I'll add coroutines to the list of ideas for upcoming videos. 🙂
@_MrKekovich
@_MrKekovich 9 ай бұрын
Your voice and explaining flow is amazing. I really like this channel.
@typealias
@typealias 9 ай бұрын
Hey, thanks so much! I'm really glad to hear that you're enjoying it!
@jackli1924
@jackli1924 Жыл бұрын
Awesome Video! Thank you Dave You always explain things deeply and concisely and easy to understand. beside, I have a question any function in Kotlin can be defined as a operator function or only the operator methods in Kotlin standard libraries can do these things. Thanks a lot for your amazing and excellent explaination!
@typealias
@typealias Жыл бұрын
Hi Jack, thanks so much - I'm glad the video was helpful! You can add the "operator" modifier only to functions with particular names. You can find the full list on the "Operator Overloading" page in the Kotlin documentation here: kotlinlang.org/docs/operator-overloading.html. On that page, each section has a table with a column heading that says, "Translated to". You can use the "operator" modifier with a function that has any name under that heading. Let me know if you have any other questions!
@jackli1924
@jackli1924 Жыл бұрын
@@typealias Thanks a lot for your quick reply! waiting for your new videos about kotlin tech. Your videos are so amazing and exlpained so concisely and deeply! that really informative.Keep doing it, waiting for more!
@puntouan
@puntouan Жыл бұрын
Thank you very much for your generosity in creating this type of content! Congratulations! I think it's a great job!!
@typealias
@typealias Жыл бұрын
Thank you so much! I'm glad to hear that it's helpful!
@jaredandrews9988
@jaredandrews9988 2 жыл бұрын
Really great video man, hope to see more!
@typealias
@typealias 2 жыл бұрын
Thanks Jared! I'm hoping to do at least a few more!
@jameslorenzen2610
@jameslorenzen2610 2 жыл бұрын
Excellent video Dave! One thing that would have been nice to demonstrate is how one can easily locate the extension functions. For example, in your last example, can one in Idea CMD+Left Click to jump to the extension functions? I would assume so. One thing that has frustrated me about the plethora of extension functions on a large project is being able to know when an extension function is being used and when it isn't. Along with being able to easily jump to the code to see what it is doing. Anyways, great job. Very high quality!
@typealias
@typealias 2 жыл бұрын
Thanks for the great feedback, James! Yes, you absolutely can command-click through to the definition of an extension function, so if you're ever in doubt about what code it'll run, that's a great way to make sure. As for knowing when you're calling an extension function, you could check your IDE color scheme (at Preferences > Editor > Color Scheme > Kotlin > Functions > Extension function call). By default, I think extension function calls are italicized but otherwise look similar to member function calls. You could change it to a color that stands out more. Just keep in mind that there are lots of extension functions in the standard library (scope functions, collection operators, etc.), so changing this setting will light those up as well!
@sanketnaik2082
@sanketnaik2082 Жыл бұрын
Hey Dave, keep them coming! I have enjoyed all of your videos thoroughly. Pure gold😍
@typealias
@typealias Жыл бұрын
Thanks so much, Sanket! 🙂 I'll keep at it!
@DaleKingProfile
@DaleKingProfile 2 ай бұрын
I have generators of data for testing. First I use companion extension to get generator for a type such as val ints = Int.generator which gives random int. If I want to limit the range I use the get extension to pass a range or even a single value val ints5To10 = Int.generator[5..10]
@typealias
@typealias 2 ай бұрын
Hey, Dale - that's a really cool idea! I love the combination approach where it can either be bound or unbound. I might have to use that in some of my tests soon.
@andyli619
@andyli619 Жыл бұрын
very very amazing and excellent video!
@typealias
@typealias Жыл бұрын
Thank you, Andy! 🙂 Glad you liked it!
@ayodelekehinde
@ayodelekehinde 2 жыл бұрын
Wow. I loved it. More please 🥺
@typealias
@typealias 2 жыл бұрын
Thanks Ayodele! 🙂 I'll keep at it!
@trido8135
@trido8135 2 жыл бұрын
This video is rlly awesome, you got a subscriber😂
@typealias
@typealias 2 жыл бұрын
Thanks so much! I'm glad you liked it! 🙂
@HoussamElbadissi
@HoussamElbadissi 5 ай бұрын
I'm a bit late here, but hopefully the limitation around extending classes with Companion objects will hopefully be lifted with the introduction of actual static functions into Kotlin. The Kotlin team seems to be pretty serious about that feature, and they'll largely replace most uses for Companion objects. That way we can do this: static fun BigDecimal.from(value: String): BigDecimal = ... I think they'll also mostly behave (on the JVM at least) like Java static functions, which might be better for performance and code size.
@typealias
@typealias 5 ай бұрын
Ah yeah, I almost forgot about that one! They had mentioned it at KotlinConf 23, but I haven't heard much about that lately. Now that K2 is out, I expect we'll start seeing a lot more action on some of these features. ( For anyone who comes across these comments and wants more background, you can read the KEEP for that topic here: github.com/Kotlin/KEEP/blob/statics/proposals/statics.md )
@HoussamElbadissi
@HoussamElbadissi 5 ай бұрын
@@typealias Yeah, exciting times ahead 😁 By the way KZbin seems to have included the parenthesis in the URL, you might wanna add a space there 😅
@typealias
@typealias 4 ай бұрын
Haha, thanks! That's not the first time I've fallen for that - putting a link inside parentheses! 😅
@JoeBernulli
@JoeBernulli 2 жыл бұрын
It's very cool. Good stuff and good teacher.
@typealias
@typealias 2 жыл бұрын
Thank you so much! 🙂
@systweaker
@systweaker 11 ай бұрын
Inspiring techniques, ta!
@CommanderSteps
@CommanderSteps 8 ай бұрын
This was really entertaining. 🙂
@typealias
@typealias 8 ай бұрын
Haha, thanks Stefan! That's what I was aiming for with this one! 🙂
@abdushakoor0099
@abdushakoor0099 2 ай бұрын
9:07 kotlin becomes cooler and cooler
@diegofarias6337
@diegofarias6337 2 жыл бұрын
Muito bom o conteúdo, sua explicação e didática são ótimos. Thank you , very good.
@typealias
@typealias 2 жыл бұрын
Thank you so much, Diego! I'm glad you enjoyed it!
@rajushingadiya2860
@rajushingadiya2860 2 жыл бұрын
Totally mind blowing sir , we want more and more and more like this tricks make it more hard then more hard then take to KSP level code . dev like me are more getting hungry after learning your those magical spell again i love it when sound "ha ha ha" after archiving magic ✨ you took totally good decision about to make video instead of blog (blog are also good) but video just bring you alive in front of us also sounds clear and cut to me when you just draw on video about contains and in are opposite. your big fan - Raju Shingadiya
@BenjaminShults
@BenjaminShults 6 ай бұрын
You actually don't need the Vehicle class anymore. You can put the default implementation in the interface itself.
@nipunshah1373
@nipunshah1373 Жыл бұрын
How come invoke() works for `()` & `{}` Does callable means () & {} both ??
@typealias
@typealias Жыл бұрын
Hi, Nipun! Actually, invoke() only works for `()`. The trick I used here was due to trailing lambda syntax - as you probably know, if the last parameter of a function (even if it's the _only_ parameter of the function) has a function type, you can omit the parentheses and just use the braces of the lambda... kind of like we'd usually do with scope functions like let, also, apply, etc. So in other words, we could have written it as double({ 12 }) instead, but I was specifically aiming to make the syntax work with just braces. That custom invoke() function just calls the lambda, and relays its result to the receiver object. Of course, this was just for amusement - I wouldn't recommend pulling these shenanigans with a real project, or else your colleagues will not be very happy! 😁
@prestonc.4875
@prestonc.4875 2 жыл бұрын
Nice! Thank you.
@typealias
@typealias 2 жыл бұрын
Thanks Preston! 😁
@gayrimesruturk783
@gayrimesruturk783 2 жыл бұрын
still works as of whatever day it is
@typealias
@typealias 2 жыл бұрын
Haha, yes - around the 1:15 mark, I accidentally kept the variable name "today" after changing the value to the Kotlin 1.0 release date. This video was actually recorded only a week or so ago. 😅
@L4szcZ
@L4szcZ 9 ай бұрын
avesome tricks!
@rcalencar79
@rcalencar79 2 жыл бұрын
Today is 2016-02-15? ;-) Great video!
@typealias
@typealias 2 жыл бұрын
Haha, I intended to update that to just say "date" - I opted to use the Kotlin 1.0 release date instead of the current date... but clearly forgot to change the variable name! 😅
@georgeshalvashvili6270
@georgeshalvashvili6270 2 жыл бұрын
Nice
@typealias
@typealias 2 жыл бұрын
Thanks! 🙂
@mefodymo7456
@mefodymo7456 Жыл бұрын
Cool!
@vengateshm2122
@vengateshm2122 2 жыл бұрын
Wow!
@IncompleteTheory
@IncompleteTheory 5 ай бұрын
And .... the obfuscated Kotlin contest is born! 🎉😂
@typealias
@typealias 5 ай бұрын
Haha, yes! I'd love to see what others come up with! 😅
@rafaelacioly3252
@rafaelacioly3252 2 жыл бұрын
🤩
@lengors7327
@lengors7327 11 ай бұрын
Thanks. I'm now gonna use these tricks to write unreadable code so then only I know what it does and cannot, therefore, be fired! So, again, thank you so much!!! (No, but for real, great info in the video 😀)
@typealias
@typealias 11 ай бұрын
Lol, yeah! At least since Kotlin is statically typed, this kind of "magic" is easier to figure out (e.g., compared to my experience with Ruby), since you can just cmd+click to definitions. But still, it can definitely be a great way to confuse your coworkers! 😅
@Ashish_singh_dev
@Ashish_singh_dev Ай бұрын
Now I'm confused whether to love this language or hate it
@typealias
@typealias Ай бұрын
Haha, you should definitely love it!
@ErikBongers
@ErikBongers 5 ай бұрын
Hmmm ... that's even weirder than JavaScript.
@typealias
@typealias 5 ай бұрын
Yeah, things can get surprisingly weird in Kotlin if you try hard!
@olehmasterluck
@olehmasterluck 2 жыл бұрын
Wow! You are a kotlin cheater! :-)
@typealias
@typealias 2 жыл бұрын
Haha, yes - it does feel a bit like cheating! 🙂
@randominternet9338
@randominternet9338 8 ай бұрын
you make it so complicated for no reason what so ever
@CrapE_DM
@CrapE_DM 2 жыл бұрын
Wow, I didn't think the `date in Month.FEBRUARY of 2016` would work without putting parentheses around the last 2. Just gotta be wary of someone creating an `infix fun Boolean.of(num: Int)` function that could make it possible to go left-to-right.
@typealias
@typealias 2 жыл бұрын
I poked around with this a little - looks like the infix functions are indeed processed left-to-right. I suppose the reason it's not a problem here is that `in` itself is not an infix function. Interesting stuff!
@rajushingadiya2860
@rajushingadiya2860 2 жыл бұрын
Totally mind blowing sir , we want more and more and more like this tricks make it more hard then more hard then take to KSP level code . dev like me are more getting hungry after learning your those magical spell again i love it when sound "ha ha ha" after archiving magic ✨ you took totally good decision about to make video instead of blog (blog are also good) but video just bring you alive in front of us also sounds clear and cut to me when you just draw on video about contains and in are opposite. your big fan - Raju Shingadiya
@typealias
@typealias 2 жыл бұрын
Thanks so much, Raju! I'm glad you enjoyed this video so much! And thanks for the feedback about it. I'll plan to make some more videos, since it seems to be an effective way to share this kind of information. 🙂
Easy SharedPreferences with Delegated Properties in Kotlin
21:18
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
Fun with Function Types in Kotlin
11:35
Dave Leeds
Рет қаралды 4,1 М.
Advanced Kotlin: Generics, Type Erasure, and Reflection Explained
23:00
Variance... without Generics!
9:01
Dave Leeds
Рет қаралды 8 М.
Kotlin Context Receivers - How to Use, Why, and Best Practice
24:22
FULL Guide to Kotlin's Inline Keyword (inline, crossinline & noinline)
16:35
Intro to Collection Builders in Kotlin
11:52
Dave Leeds
Рет қаралды 4,8 М.
5 Fatal Coroutine Mistakes Nobody Tells You About
18:15
Philipp Lackner
Рет қаралды 90 М.
Applying the Decorator Pattern in Kotlin
12:25
Dave Leeds
Рет қаралды 6 М.
Advanced Golang: Generics Explained
13:37
Code With Ryan
Рет қаралды 71 М.
UFC 310 : Рахмонов VS Мачадо Гэрри
05:00
Setanta Sports UFC
Рет қаралды 1,2 МЛН