Kotlin Scope Functions Tutorial - let, run, with, apply and also

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone in this video we will learn about the  scope functions of Kotlin to make our work easy   Kotlin standard library has many functions to form  a temporary scope we can call these functions from   any object and inside the temporary scope that is  formed we can access the object to perform more   operations on that object there are five scope  functions available in standard library and all   these functions are very similar in nature because  all these functions forms a scope where you can   access the object we can differentiate these scope  functions in two ways the first one is how it   refers the context object and the next thing is  what is the return value of this scope function   this table here defines how the context object is  referred in each function and what is the returned   value as you can see the first three functions  let run and width returns the lambda result   and the last two apply and also returns the  context object itself and inside let and   also we can use it that is the default name  given to the lambda argument but you can also   give your custom name if needed all the other  functions refer the context object using this   it means you are inside the object scope and  you can refer to the members using this keyword   and if you want you can also omit the this  keyword and you can directly access the members   if the context object is referred by this  that means if you're inside run with or apply   so one by one we will see examples of  these scope functions the first one is let   and for the sake of example I have already  created a class here so we have a class square   we have some properties and we have  some functions inside this class   now we will use this class to explain the  scope functions so we will start with let   now we can use led to form a temporary scope and  inside the scope we can access the object and this   statement is true for all the functions available  here so we will start with let for example I have   val square and I can create a square that  is our class i will pass with and height   and to call the scope function let  i can write a dot here and then let   now inside this lambda that is a block of curly  braces a scope is formed and inside the scope we   can access the square instance that is created as  you can see with the hint here we can access the   object using it keyword so here I can simply write  it dot fill color let's say I want to fill red and   then it dot add text and I want to add some text  like this now if you want you can change the name   of the argument that is it by default so here  we can simply write any name let's say square   like this so now instead of it you can use square  and as I already shown here the return value of   let function is the lambda result and it means  the last statement that you will write inside   this scope is the return value and it is optional  if you want to use the return value you can use it   if you don't write anything that means it  is returning unit so I want to return this   square so what I can do is I can write a square  here and the last statement is the return value   now if i will print the square here i am printing  this square let's run the code you can see   we are getting these functions called for example  fill color red color filled and then add text   some text text added and finally we are  getting the class value where the stand   height is 20 color is red and text is some text  because here i am printing the instance and I   have overridden the Tostring function so that is  why I am getting the string value of the class   so this is what the let function is and very often  we use this let function for null safety or for   checking if the value is null for example  we can create a new square here let's say   I have latent where square of type square and  inside my main function I have a null square.  Now assume square is coming from somewhere   and it can be null right now it is null because  it is not coming from somewhere but let's assume   it is coming from some other function and it may  be null or it may not be null so in this case we   use let function very frequently for example if I  want to assign this square let me change the name   square 1 to this square then what I  can do is I can write here square 1   let and then square equals to it so this is  how we use the let function very frequently   so this is all for let now let's go to run run  is same as let it returns the lambda result   but the context object is referred using this  and not it so in case of run we cannot have   a custom name for our argument because it is  this and we are inside the scope of the object   so let's see how it works i will delete everything  and this time i will create val square equals   2 square the same thing actually let's say  10 20 and then i will use the run function   now the square is getting referred using this  as you can see with the help of this hint here   so here you can use this this dot add text  let's say some text this dot fill color   let's say blue this time and finally we can return  this because it also returns the lambda result   that means the last statement is the return value  and you do not need to write return explicitly and   because it refers the object using this this is  also not necessary we can omit the this like this   and if I will print the square then I will get  the same output you can see it is working fine   so we are done with let and run let's  use width now we use width when we have   a lot of functions and properties to  call from an object so what happens is   you created an instance and you need to  call functions from this instance let's   say fill color red square dot add text my text  or anything and you have a number of functions. okay let's say f1 and then you have square dot  f2 and you have a lot of functions let's say 10   20 functions now you can simplify the code using  width what you can do is you can pass your object   inside with function and it will form a temporary  scope where you can refer the object using this   that means you can directly call all the functions  and directly access all the members of this square   instance without referring the instance again and  again and again and with returns the lambda result   that means the last line is the return value or  if you don't want to use the return value you can   ignore it it's not necessary it is optional  so here I can use square dot add text let's   say my text or anything square dot add color it  is fill color so let's say yellow or anything. Now I can print the square and I will get  the same result you can see it is working   so we are done with width now we have  apply now it refers the object using this   and returns the object itself so you can  simply use apply like this we have square   and then apply so apply we can use to perform the  initial configuration of an object for example   we created an object and we are applying these  changes for example I want to fill the color let's   say black and I want to add the text black text  or anything and finally we are printing the square   here we do not need to return the square because  it returns the context object itself so here we   are getting the square that is modified inside  this apply function now if i run the code I will   get the same output we are getting the function  called and we are getting the updated values   now the last one is also and it is same as  apply and you can use also as well for initial   configuration the only difference is inside  also you will use it to refer the object. Like this the code and we will  get the same output you can see so this is how you can use scoped functions   and in case you have any question or confusion  you can leave your comments I hope you liked   this video and learned something in case  you have any suggestion or feedback you   can leave your comments below and also  for any kind of question don't hesitate   to ask by commenting below so thanks for watching  everyone this is BELAL KHAN now signing off. you
Info
Channel: Simplified Coding
Views: 4,558
Rating: 4.7480316 out of 5
Keywords: kotlin scope functions, kotlin let, kotlin run, kotlin with, kotlin apply, kotlin also, kotlin tutorial, androiod kotlin tutorial, kotlin for android, kotlin object oriented programming tutorial, kotlin for beginners
Id: oL9NZWpKEaU
Channel Id: undefined
Length: 11min 58sec (718 seconds)
Published: Sat Jan 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.