Тёмный

Inline functions - Kotlin Vocabulary 

Android Developers
Подписаться 1,3 млн
Просмотров 82 тыс.
50% 1

Опубликовано:

 

11 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 142   
@germainkevin5987
@germainkevin5987 4 года назад
If you're an Android java developer that understands nothing about the kotlin codes in this video, show yourself , you're not alone
@ArturStaniec
@ArturStaniec 4 года назад
I understand there might be restrictions in your job, but why don't you guys start using it in your own projects? The earlier you start the better and, in my option, the transition is inevitable when java samples or resources start to be neglected by Google.
@germainkevin5987
@germainkevin5987 4 года назад
@@ArturStaniec well we're taking that into consideration as we realize Kotlin is getting more and more popular and pushed by Google, it's just a joke to make people like me who have not fully mastered the logic behind the language 😂
@nadeemshaikh7863
@nadeemshaikh7863 4 года назад
@@ArturStaniec Imagine companies transitioning fully to kotlin and then, Google comes and announces kotlin is out and dart/flutter is in!
@areebjamaliam
@areebjamaliam 4 года назад
@@nadeemshaikh7863 It may be that you're joking but that is actually impossible. Flutter and Dart are completely independent runtime and implementation of components where Kotlin is just a language which compiles down to Java Bytecode essentially making it same as Java and using the native platform APIs and components
@nadeemshaikh7863
@nadeemshaikh7863 4 года назад
@@areebjamaliam You didn't get my point. I'm saying Google could or won't (more likely) to make flutter as the preferred way to make Android apps which would technically be fuchsia apps if Google transitions to fuchsia OS.
@Zhuinden
@Zhuinden 4 года назад
Good video, great demo of the Kotlin Kotlin bytecode Java decompiled code, nice editing work too! Would be nice to also see `crossinline` for lambdas that are invoked inside another lambda
@PauxloE
@PauxloE 4 года назад
If the rule is that simple (small functions which take a lambda, but don't pass or store it), why can't the Kotlin compiler decide by itself whether to inline a function or not?
@deleater
@deleater Год назад
Because inlining adds extra bytecodes to your apk (increase apk size). Some prefer smaller apk size because for them the runtime overhead isn't a problem. I think this is why they provided it as a keyword as an option.
@davidespano8674
@davidespano8674 2 года назад
"...but only if you think we've earned it!" You surely have, not many say it. Commendable.
@AndroidDevelopers
@AndroidDevelopers 2 года назад
We're glad you think so Davide. Thank you for the support!
@LiranBarsisa
@LiranBarsisa 4 года назад
2:34 You say it's not recommended to inline a function that doesn't have any function as parameter, but then on 2:52 you show so many functions that are inlined even though they don't have a function as parameter... What is going on?
@zeusalmighty6740
@zeusalmighty6740 3 года назад
I hope you found the answer. Still I’ll clarify, the inline functions show at 2:52 have lambdas as parameters which is just a fancy way of writing a function.
@LiranBarsisa
@LiranBarsisa 3 года назад
@@zeusalmighty6740 What do you mean? Those are very simple functions, and they could be auto-inlined anyway during runtime. What's so special about them?
@zeusalmighty6740
@zeusalmighty6740 3 года назад
@@LiranBarsisa If you pass in the lambda, each time a new function is created, this would make it so it only occupies memory once.
@LiranBarsisa
@LiranBarsisa 3 года назад
@@zeusalmighty6740 Please explain. She said to avoid this in these cases, and yet I see "inline" for them. Why is that? Also, explain how could this be better than not putting "inline"? Show a sample snippet in which it behaves differently (with "inline" vs without it).
@zeusalmighty6740
@zeusalmighty6740 3 года назад
@@LiranBarsisa Plain kotlin won’t be enough to explain this. These are optimisations at byte-code level. I recommend you to make two functions one inline and other not inline and both take a function as parameter. Check the decompiled byte-code and also you can check the memory it uses.
@arseniykucherenko4960
@arseniykucherenko4960 4 года назад
How about crossinline and reified? Why you missed such an important keywords of inline functions Kotlin ecosystem?
@rizwansayyed7893
@rizwansayyed7893 2 года назад
reified keyword use to find out class parameter type name in compile time
@9980500125
@9980500125 4 года назад
crossinline?? 🤔🤔
@JolanRensen
@JolanRensen 4 года назад
I only came across this when accepting a suspend lambda as an argument in an inline function. I suspect it would be preferred over noinline but I'm not sure. Explanation would be really helpful!
@iRYO400
@iRYO400 4 года назад
@@JolanRensen I did the same, then tried to decompile that piece of code. It took about 40 mins of 100% CPU usage. At the end, I got java class which weight about 320 MB while origin was 2 KB :D
@Zhuinden
@Zhuinden 4 года назад
@@JolanRensen short answer, you need `crossinline` if you are passing in a lambda that you are invoking inside another lambda
@ianmubangizi6721
@ianmubangizi6721 4 года назад
Good to see her hand is healed now Great short videos 👍
@diegonovaes
@diegonovaes 2 года назад
Very good explanation! Thanks for sharing
@AndroidDevelopers
@AndroidDevelopers 2 года назад
Thanks for the kind words, Diego. Be sure to check out even more Koltin vocabulary with this playlist: goo.gle/2wHr94N 😎
@isaaclacoba4458
@isaaclacoba4458 3 года назад
Kotlin meets C++. Nice to have this ""low level features"" in a high level language.
@denysm.9968
@denysm.9968 3 года назад
very true, Kotlin gives flexibility of the C++ like inline, coroutines and so on, next big step will be manual alloc/dealloc objects and reference counting - really dead end. Just my subjective opinion - Kotlin should be clear and easier to understand then Java but on practice - Java is much more readable and has less complicated constructions.
@goobar
@goobar 4 года назад
Thanks Florina 👍👍
@MerthanE
@MerthanE 4 года назад
As a Kotlin GDE, can you tell me why the compiler itself doesn't take care of this itself? Can't it figure out when functions should be inlined to gain the performance benefit?
@goobar
@goobar 4 года назад
@@MerthanE That's a good question. To some extent, the tooling can give some hints as to whether or not a function being inlined will help. This comes in the form of lint warnings that can let you know if there will be a performance benefit or not from adding the inline modifier. Beyond that, in the realm of actually optimizing the code for us, I'm not sure. One challenge with this could be that the compiler doesn't know how a function will be used, and therefore can't necessarily make an educated decision about whether to modify the code for us. A large function, inlined once, might have minimal impact to performance and generated code size. Or, that same function might be used many times and greatly increase the amount of generated code. Because of that, perhaps that tradeoff is simply left to the developer?
@MerthanE
@MerthanE 4 года назад
@@goobar makes sense as developers know how functions will be used in the future, but at the same time... the compiler should know how functions are called while compiling so dynamically making them inline or not seems like it would be possible. But this is pretty specific, no idea how the compiler actually handles this, just thought that this might be one of the things that are possible improvements under the hood. I also don't know examples where the compiler couldn't figure it out (but a developer could) but there might be some
@sebas8824
@sebas8824 4 года назад
This explanation was way less complicated than Coursera´s course for Java developers. Thanks a lot.
@me-manikanta
@me-manikanta 4 года назад
Awesome explanation!! Anyways, Is there any place where we can request for explanation of certain topics😅
@bagadeshkumarr9502
@bagadeshkumarr9502 4 года назад
if there is a pole then we vote.
@AmCanTech
@AmCanTech 4 года назад
Stackoverflow
@florinamuntenescu8970
@florinamuntenescu8970 4 года назад
Reply on my Twitter question: twitter.com/FMuntenescu/status/1219222484542808065?s=20
@areebjamaliam
@areebjamaliam 4 года назад
@@bagadeshkumarr9502 How long is it?
@bocckoka
@bocckoka 3 года назад
from what I've heard, the heuristics that LLVM uses to decide whether it should inline a function is 'yes'. Kotlin works differently it seems
@codeblooded
@codeblooded 4 года назад
This series is awesome. Hope we can see similar tidbits for other languages too.
@Skater901
@Skater901 4 года назад
So would it be accurate to say that the trade off of using the inline keyword is between performance and compiled code size? Inlining your functions can increase performance as the lambdas don't need to be instantiated as objects at runtime, but the size of your inlined function and the number of places you're using it both contribute to increasing the total size of your compiled code?
@martinseal1987
@martinseal1987 4 года назад
But if you made that shared Prefs instance a singleton would it not be the same?
@it-6411
@it-6411 4 года назад
Oh yeah, there are nothing was said about reified types and crossinline: the first - it’s compile-time known generic type, using in most cases as “Class” type replacement, so convenient, the second one used to pass inlined blocks to another , new created objects inside of function (for example, pass block to new Runnable instance inside function)
@Daaaaaaavid
@Daaaaaaavid 4 года назад
Your slides are sometimes too short on screen, like on 0:52, it's shown 1 second and bye, you are too fast sometimes!
@krokenstiv8777
@krokenstiv8777 4 года назад
Google does not need Kotlin. Google needs IntelliJ for Android Studio. But the point is that both Kotlin and IntelliJ are the products of the same company...
@charfiamine5523
@charfiamine5523 3 года назад
The example you picked is not obvious for beginners IMO, you could have picked an easier one. A part from that, great explanation.
@genctasbasi
@genctasbasi 4 года назад
I'm a bit more inclined to use the inlined functions now.
@victoraceves7732
@victoraceves7732 4 года назад
Awesome explanation! Thanks :)
@vikaspandey7053
@vikaspandey7053 2 года назад
music at the end is not required. 4:42
@mohsenss8791
@mohsenss8791 4 года назад
I wrote exactly the same method in Kotlin 1.3.50 and 1.3.72. The decompiled java class is different. It uses an interface and an invoke method. Also, by using the inline keyword, there is no difference in the decompiled java class because of using that interface. Even, there is no IDE guidance to remove it
@amir32806
@amir32806 4 года назад
Why we can't gain performance improvement if the function doesn't use other functions as parameters?
@goobar
@goobar 4 года назад
I might be able to help answer that... When we create a higher order function, each function parameter is treated as an instance of a Function class in the underlying bytecode. This means, each time we invoke the higher order function, we are instantiating a new object. If we are operating on some large collection, this could means 100s or 1000s of allocations. By inlining the function, we avoid the need for this Function class allocation. So for non-higher-order classes, this performance increase doesn't apply. And for the regular execution of the code, the compiler already does a good job optimizing so the gains are minimal.
@amir32806
@amir32806 4 года назад
@@goobar Thank you for your answer! I thought I already knew the *inline* keyword from C++, and didn't even wanted to watch this video. 😀
@THOMASM00777
@THOMASM00777 Год назад
How to setup to see the kotlin byte code in android studio?
@romanpavliuk2301
@romanpavliuk2301 4 года назад
Thanks for the video! But about crossinline keyword?
@Zhuinden
@Zhuinden 4 года назад
short answer, you need `crossinline` if you are passing in a lambda that you are invoking inside another lambda
@romanpavliuk2301
@romanpavliuk2301 4 года назад
@@Zhuinden Thanks hope others will see your comment
@eimaisklhros
@eimaisklhros 3 года назад
This is nice and all, but I find myself missing how the ins and outs of the compiler work and I don't really understand some stuff about object creation. Therefore, I miss the problem inline functions are trying to solve... :|
@rubyh4184
@rubyh4184 4 года назад
Compare with java? Java automatically does it right?
@ianmubangizi6721
@ianmubangizi6721 4 года назад
The day I learnt to use inline functions as extensions, I was like wow!!!
@martinseal1987
@martinseal1987 4 года назад
I will start using kotlin as soon as my project is done but honestly apart from kotlin UI and soon being able to use it to create swift code I don't see the appeal reminds me more of JavaScript and I don't particularly like it
@Zhuinden
@Zhuinden 4 года назад
Extension functions, functional types as part of the language (without being bound to minSdk 26), and maybe typed nullability
@Zhuinden
@Zhuinden 4 года назад
But for a more... contrived, but real? example, this one line of code (github.com/Zhuinden/livedata-combinetuple-kt/blob/master/livedata-combinetuple-kt/src/main/java/com/zhuinden/livedatacombinetuplekt/LiveDataCombineTuple.kt#L752 ) would be 16 lines of code EACH in a Java file. So I personally also really like tuple decomposition for data classes (where applicable).
@JayDonga
@JayDonga 4 года назад
I do not understand meaning of this line action: SharedPreferences.Editor.() -> Unit
@yoelglus
@yoelglus 4 года назад
It is an extension function on the SharedPreferences.Editor class that accepts no parameters and returns nothing. The implementation of this function will be passed as a parameter to the edit method.
@Gg-is2jg
@Gg-is2jg 4 года назад
search for lambda with receiver, & i prefer to read first about Extension functions.
@jamesje577
@jamesje577 4 года назад
Awesome mam❤
@rizaanjappie
@rizaanjappie 4 года назад
The color scheme in the code makes it look boring. Also is it good for the eyes long-term?
@mursalcabdurahman7120
@mursalcabdurahman7120 4 года назад
Good afternoon 🌞🌞🌞
@grottordrynor4546
@grottordrynor4546 Год назад
I started Kotlin some weeks ago, I don't get this stuff right now but hope I will later.
@AndroidDevelopers
@AndroidDevelopers Год назад
Hey there! We'd love to lend a helping hand 😁 Have you checked out the Android Basics in Kotlin course? There are even earnable badges, so you can show off your Kotlin love 😍: goo.gle/3ekZInO
@youdube1203
@youdube1203 4 года назад
kotlin would be best lang in future
@ss-xn5rw
@ss-xn5rw 3 года назад
plz make a tutorial video on kotlin scope functions, it is very confusing 😕
@CriticasDeCriticas
@CriticasDeCriticas Год назад
Me watching this: Awesome, can't wait to use it. Also: Lol
@AbdulelahAGR
@AbdulelahAGR 4 года назад
Thanks for the explanation. And great that your hand had recovered.
@ozgurtas4508
@ozgurtas4508 4 года назад
nice one
@kamertonaudiophileplayer847
@kamertonaudiophileplayer847 4 года назад
It sounds like copy/paste, right? Many developers will love it.
@noraltavir
@noraltavir 3 года назад
Using inline function everywhere does not improve performance, but on the contrary, decreases it because JVM inlining is better than manual inlining. Also, using inline everywhere significantly bloats the bytecode because it creates a copy of the code for each call. So please, PLEASE, do not recommend using inlines for optimizations. And it definitely should not be ever used in large functions. Performance optimizations like that should not ever be used without need and without profiler measurements.
@nvrthles
@nvrthles 4 года назад
Why is there no performance gain when making a function that doesn’t accept other function inline ?
@VLEIX
@VLEIX 4 года назад
because if the inline method receives just one parameter, which needs an instance, this must be noinline. Therefore, an instance will always be created every time the inline method is called. Check the min 3:39 :)
@emmanuelmtali1594
@emmanuelmtali1594 3 года назад
crossinline?????
@orangeboys8901
@orangeboys8901 4 года назад
I like kotlin
@imhbhh
@imhbhh Год назад
@denysm.9968
@denysm.9968 3 года назад
Kotlin crew has invented the ugly fix on what should work under the hood and continue extending the set of operators saying: "Kotlin is so idiomatic language". As an application developer I should think about Business Rules instead of: "Inline or not inline"? For me the whole picture looks like the following way: "I shoot my leg and lets make a party it is so cool" - No, guys -> its better to go to the hospital. The next step is to add C++ pointers and start allocate/deallocate objects manually saying it's so powerful and provides a lot of flexibility - joke. I've seen a lot of projects and I would say there is no consistency between them, because on trivial tasks each developer wants to demonstrate how does he know Kotlin and uses all his knowledge to make simple solution really complex and thats a real pain for those who will inherit the project and those who will join. That's a real shame. On the other hand Coroutines is a big win and thank you for that.
@_maverick.
@_maverick. Год назад
Sounds too complicated and time consuming while you're coding, as you need to focus on the code logic, while paying attention to these minor details becomes a distraction. Kotlin should take care of it by itself under the hood
@it-6411
@it-6411 4 года назад
I don’t understand people, which don’t use Kotlin in their projects in 2020: come on, guys, Kotlin is official Android development language already for more than 2 years. Also, it’s pretty more beautiful, more convenient, than Java. You just need to exit from your “comfort zone”)
@SanketBhat7
@SanketBhat7 4 года назад
First comment😊. Helpful knowledge👌 keep it going 👍
@shivamdawar97
@shivamdawar97 4 года назад
None a single thing I understand about inline function.
@youdube1203
@youdube1203 4 года назад
inline function look like lazy initialization
@areebjamaliam
@areebjamaliam 4 года назад
No
@hamzahalhajji7032
@hamzahalhajji7032 4 года назад
I want to know why Google give us two new language like kotlin and dart
@hamzahalhajji7032
@hamzahalhajji7032 4 года назад
@BattleNo0bFaiLTV It's look like you made it more complex
@goobar
@goobar 4 года назад
Huge company with multiple teams all working on different projects. I'm fairness too, Google didn't create Kotlin, they simply decided to make it a first party language for Android after the community decided they wanted it.
@hamzahalhajji7032
@hamzahalhajji7032 4 года назад
thank you Mr Goobar You are really fairness but as Mobil developer it is really difficult to decided which language I should master with all this Features in every language
@JayDonga
@JayDonga 4 года назад
Kotlin is not created by Google, it's created by JetBrains.
@areebjamaliam
@areebjamaliam 4 года назад
​@@hamzahalhajji7032 Uh, both are completely different things. Kotlin is for easing Native Android Development and uses the underlying platform and components and Flutter/Dart is for multiplatform development using their own rendering engine to show the UI. The choice is not between Kotlin and Dart at all. It is between Android Platform SDK or independent cross-platform rendering engine which completely ignores prior art and reimplements and mimics the underlying system. The choice is yours. One may lag behind and even get deprecated in future (flutter), where the other is literally the Android Platform itself. If in an impossible scenario, Kotlin gets deprecated, you can de-kotlinify it and convert it back to Java. Which you'll never be able to do in Flutter/Dart. Deprecation of the Platform SDK would mean the end of Android Platform itself, so it will never happen. And if it does, then Android will not be present itself, so Flutter v Native will be irrelevant
@ChristopherNeurofunker
@ChristopherNeurofunker 4 года назад
Or make it on Java simply :V
@areebjamaliam
@areebjamaliam 4 года назад
Then you get same performance overhead with higher order functions (anonymous classes) with no keyword like inline to optimize it and bail you out
@Zhuinden
@Zhuinden 4 года назад
Java would create the new object instance with no way out. Kotlin's solution is better in this case
@moustafniama6332
@moustafniama6332 4 года назад
@@Zhuinden xc. 🛁
@attalaw36
@attalaw36 4 года назад
very complex beginning so I don't like this lesson ...
@nicklasnilsson8217
@nicklasnilsson8217 3 года назад
This is really messy
@danielnovak5668
@danielnovak5668 2 года назад
Way too fast for me to be honest.
@mondoshigua
@mondoshigua Год назад
📱🇨🇴🙋🏻‍♂️👍🏼🤝🏼
Далее
Object - Kotlin Vocabulary
7:53
Просмотров 32 тыс.
Suspend functions - Kotlin Vocabulary
9:03
Просмотров 50 тыс.
A small kitten was dumped #cat #kitten #cutecat
00:41
Что думаете?
00:54
Просмотров 790 тыс.
Decompress small game, have time to play it!
00:35
Navigation Compose meet Type Safety
16:50
Просмотров 7 тыс.
Collections and sequences - Kotlin Vocabulary
5:18
Просмотров 40 тыс.
Let, Also, Apply, Run, With - Kotlin Scope Functions
11:44
Cursor Is Beating VS Code (...by forking it)
18:00
Просмотров 73 тыс.
Building for the future of Android | Spotlight Week
22:22
Flow layouts | Jetpack Compose Tips
4:18
Просмотров 4,6 тыс.
Shaders | Jetpack Compose Tips
5:56
Просмотров 9 тыс.
Kotlin Coroutines 101 - Android Conference Talks
24:49
Просмотров 135 тыс.
A small kitten was dumped #cat #kitten #cutecat
00:41