Let, Also, Apply, Run, With - Kotlin Scope Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to a new video in this video i want to talk about the different kotlin scope functions so we have let also apply and run and there's also width but that's really the same as run because i feel like many of you who just started with kotlin don't really know the difference between these functions and they just find these very confusing and i just want to show you in which cases you should basically choose which function here and i want to start with the most commonly used one which is let and we usually use led for null checks in kotlin so let's say we have a variable here private var number nullable integer and we set it to now so you might think now why can't we just choose an if condition to check for null here so if we use if number is equal to it's not equal to null then let's say we want to choose we want to create a second variable here val number two and that is just equal to number plus one you can see we get an error here smart cards to end is impossible because number is a mutable property that could have changed by this time so why does this actually happen because we checked if number is actually not equal to null why does it give us this error here so the problem here is we declared this number variable globally that means we don't really know here at this point if there are other threats accessing that number at the same time so what could happen here is um that we check if number is not equal to null okay it is not equal to null we go inside of that if condition but before we actually enter this variable declaration here another thread actually sets this number back to null so then we would still be inside of that if condition but that number would be null and since kotlin is a null save language this is something that can't happen here because whatever happens here in our code whatever variable we want to access we must be sure that it can never be now and one way to solve this here is to simply assert this number is not equal to null here then this error goes away but we don't really want this because still if other threats access that number and set it back to null when we're inside of that if condition then this would simply crash here because this operator will just make our program crash if we if it is null so this can't really be the solution here the solution is actually the let keyword so what we can do is we can actually let's leave this number 2 here and just replace this if condition here with number question mark dot let and then we replace this number with it and that now works why does that work so we only execute that led block if number is actually not equal to null that's what this question mark operator does here in kotlin and then we have access to this it which is basically just number and in this case what this let function will do is it will kind of save this number at the time we actually called this let function so if we change number during that led block then this won't affect this it so that is basically just the state of that variable of that of that object here at the time we call that that function and that's why this works now and you can also use this lat function basically as an assignment to a variable so if we declare a variable here and set that equal to this lab block so variable x for example then this works if we hit control q on that x we can see that's of type unit and the reason that is of type unit is because that led block just returns the last line of it and since this is just a variable declaration this is basically of type unit we could for example simply write a number two in the last line and then x would simply get whatever is saved in number two so if we now hit ctrl q here that is of type integer and of course that is still nullable because if number would be null here then we wouldn't even go inside of that led block then we would simply assign null to this x variable but that is basically when you should use let every time you need to make such a null check that would otherwise throw an error and if you want to use the else case so if this would simply return null then you want to assign something else then you can do this with this elvis operator here for example just assign 3 to x then we can get to the next scope function which is also also is very similar to let so you can also just call that on whatever object you like and you simply get a reference a parameter here inside of that lambda block to the object you actually call that also function on but the difference to let is that also does not return the last line as led does it instead also we'll return the object it was called on so let's say we declare another variable here private private bar i set that to zero and we write a function here get squared i and that function would simply square whatever is saved in i let's say you also want to increase i by 1 after you actually make that calculation inside of that function then you could simply put an also block here so we also want to do something else and increase i by one so we can now still use that single line expression here and set that equal to or set that function equal to that expression rather and still actually do two things with or actually two and still actually do two things here with i so on the one hand we square it and then we also increase it by one and if we would use it here so you can see we again get it that would just reach uh refer to this i squared but if you want to increase this i we would simply need to use this i here as well and as i said the difference to let is that also we'll now return the object it was called on so it will re this function will actually have the result of this i squared that is the object also was called on and not at the last line of this also block so this is actually not that often used as let but i still want to explain the difference here and now we get to another scope function that i use very often which is apply so apply can also just as every scope function be called on every object in kotlin and apply is super helpful to just um modify objects so if you need to make a lot of changes on a specific object then you should use apply so let's say we create an intent object here and we use apply afterwards then you can see this time we get this reference here to this intent and not in it reference so we don't need to prepend it all the time if we want to refer to this intent instead it is as we would be inside of an intent class here so we can call all the intent functions directly without putting any variable name before that so we could put put an extra here what we frequently need to do with intents and for example a string extra we can put another extra for example an integer oops an integer extra we could set the action of that intent to something and that will now simply return the intent here with all these functions and all these changes applied so we could simply save that in an intent variable and then for example start an activity with that and that is also very useful let's say you have a person class and you want to change a lot of properties of a specific person then you can just use that person call apply and directly set the name the last name the age to whatever you like without writing person.name is equal to person.lastname is equal to person.h is equal to so you get what i mean here and now we get to the last function which is run so now with this apply function you can see this returns an intent here it simply returns the intent with all the actions applied if we use run now that will also give us such a this reference to that intent and now check that intent object here with control q now this is of type unit because run is basically the equivalent to apply the same way as let is the equivalent to also so that means run will do the same as apply but it won't return the object it was called on instead it will return the last line because that is a unit here this intent will be of type unit so if you would write this here which would be would not be very good here because we should rather use apply here but this now is the last line and this refers to this intent then this is of type intent again so run is really nothing i use very often here i use apply much more often because you want to apply a lot of changes here to a specific object but then return the object you actually wanted to put these changes on there's also with which i never used in my life because it's the same as one just a different signature so we could use with here and then a new intent object and then do something with that intent object and we still get a reference to this intent that's the same as run and i prefer run which i also don't need that often so these were the five kotlin scope functions i hope this was understandable for you if not put your questions in the comments and if it was then make sure to hit the subscribe button and give this video a like and if you're looking for more advanced android courses then check out pl minuscoding.com which is my website that you will find more advanced courses just check it out you will like it you will also get a bunch of free stuff there just by creating an account so thanks for watching see you next video bye bye [Music] you
Info
Channel: Philipp Lackner
Views: 76,639
Rating: undefined out of 5
Keywords: android, tutorial, philip, philipp, filipp, filip, fillip, fillipp, phillipp, phillip, lackener, leckener, leckner, lackner, kotlin, mobile, android studio
Id: Vy-dS2SVoHk
Channel Id: undefined
Length: 11min 44sec (704 seconds)
Published: Sun Nov 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.