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

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ต.ค. 2024

ความคิดเห็น • 179

  • @AlaaAlbrg
    @AlaaAlbrg 3 ปีที่แล้ว +234

    Summary of Scope functions:
    let: Used to check nulls, also better than simple null check in multi-threading case
    also: same as 'let' but it doesn't return the last line as 'let', instead 'also' will return the object it was called on and 'not the last line!'
    apply: helpful function to modify objects, if you want to change in properties of the objects, and it uses 'this' instead of 'it' as we work inside the class of the object
    run: equivalent to 'apply', but it won't return the object it was called, instead it will return the last line
    with: same as 'run' but a different signature.

    • @PhilippLackner
      @PhilippLackner  3 ปีที่แล้ว +11

      good!

    • @trungtrankim294
      @trungtrankim294 3 ปีที่แล้ว +1

      Awesome. Thanks a lot man.

    • @rithulraj3348
      @rithulraj3348 2 ปีที่แล้ว +1

      *with()* is the combination of *let* and *run* because it does null check also, am I right?

    • @hazfrd
      @hazfrd ปีที่แล้ว +1

      @@rithulraj3348 nope. In fact run is combination of with & let. Run does check nullpointer (let) and reference with this (with) , after that return the last line a.k.a lambda expression

    • @hazfrd
      @hazfrd ปีที่แล้ว

      @@rithulraj3348 with() doesn't checking null, just for non-null. in case it is null then use let(), in case you want make some operation while checking null then use run()

  • @codinginflow
    @codinginflow 3 ปีที่แล้ว +63

    11:04 sick special effects bro
    .also {
    respect++
    }

  • @agp1444
    @agp1444 3 ปีที่แล้ว +52

    also is very useful in case x = y.also { y = x } - switch values of variables without third variable

    • @mahdi7236
      @mahdi7236 2 ปีที่แล้ว +1

      big brain time😉

    • @TheMohit987
      @TheMohit987 2 ปีที่แล้ว

      😂😂😂😂 wow

    • @hieuluu2495
      @hieuluu2495 2 ปีที่แล้ว

      Awsome

    • @phamhung2263
      @phamhung2263 ปีที่แล้ว +2

      why don't we just switch those variables with a third variable. It's more readable

    • @disgruntled_llama
      @disgruntled_llama 2 วันที่ผ่านมา

      @@phamhung2263 or write a function called "swapValues(x, y)" and use that. That's the most readable.

  • @damercy
    @damercy 3 ปีที่แล้ว +13

    Hey man. I just learnt Kotlin. Previously I've made a few basic projects in android using Java. Fortunately,I found your channel with some really good resources to learn android dev in Kotlin. Just wanted to say a genuine thank you for putting out such great content. Please don't stop. Power to you! 🔥❤️

    • @PhilippLackner
      @PhilippLackner  3 ปีที่แล้ว +2

      Thanks! You're totally welcome

  • @firuzaaliyeva5382
    @firuzaaliyeva5382 3 ปีที่แล้ว +10

    Was literally just thinking about finally studying these, and you just uploaded. Thanks!

    • @iJuce
      @iJuce 3 ปีที่แล้ว

      same wtf

    • @PhilippLackner
      @PhilippLackner  3 ปีที่แล้ว +7

      yes, I can read your mind

    • @mz-coder
      @mz-coder 3 ปีที่แล้ว

      @@PhilippLackner LOL!

  • @benjaminmorales8624
    @benjaminmorales8624 3 ปีที่แล้ว +12

    You’re an amazing dev, thank you for all this effort and please keep it rocking!

  • @mirzasohailhussain
    @mirzasohailhussain 9 หลายเดือนก่อน

    Thanks Philip, you are sharing so much knowledge without any charge. I have seen so many people share only basics and then member only videos

  • @larrystreehouse1124
    @larrystreehouse1124 ปีที่แล้ว +1

    Simple and Concise explanation. Even though I go self-learning with Kotlin for 1 year, this is great to recall my memory and consolidate my knowledge. More importantly, you show us how to teach and explain the basic concept in an interesting and concise passages with live examples.

  • @weather120widget8
    @weather120widget8 ปีที่แล้ว +2

    I really appreciate your teaching style, how you build on topics as you go. Keep up the great work.

  • @2chotti
    @2chotti 21 วันที่ผ่านมา

    This is the best video on scopes. Thanks

  • @ganshakumari5196
    @ganshakumari5196 2 ปีที่แล้ว +2

    Fantastic explanation. Much clearer than most of the online resources, Thanks Mate!

  • @FatalRescue
    @FatalRescue 3 ปีที่แล้ว +3

    Wow, thank you for opening my eyes. I was just using let as shorthand for the null check and did not realize how useful it actually is!

  • @ziakhan-tk7rk
    @ziakhan-tk7rk 2 ปีที่แล้ว

    Your tutorials are addictive I watch them repeatedly

  • @salimmazariboufares3118
    @salimmazariboufares3118 3 ปีที่แล้ว +1

    Cool, that was pretty much understandable by someone who is new to Kotlin.

  • @TheMohit987
    @TheMohit987 2 ปีที่แล้ว +1

    Your videos are very helpful. Your way of teaching is simply awesome.

  • @usherg4158
    @usherg4158 3 ปีที่แล้ว

    o man, I have been reading lots of explanation and still not get it until watching yours. Great example and explanation, thanks for making this video.

  • @FreeKnowledge
    @FreeKnowledge 2 ปีที่แล้ว

    Fantastic explanation.

  • @ianlipa9826
    @ianlipa9826 10 หลายเดือนก่อน

    Your tutorials are a big help bro. Thanks!

  • @achalbadgujar9519
    @achalbadgujar9519 3 ปีที่แล้ว

    Finally a practically helpful explanation of these scope functions

  • @carloscambon5402
    @carloscambon5402 ปีที่แล้ว

    You explain very clearly. Thank you.

  • @thearpansircar
    @thearpansircar 3 ปีที่แล้ว +1

    An absolute psychic. I was going through Kotlin's Documentation and, suffice to say, it confused the fuck outta me.

  • @jackli1924
    @jackli1924 ปีที่แล้ว

    Awesome video!You explained very simply and concisely. Thanks a lot Philipp!

  • @aweklin
    @aweklin 3 ปีที่แล้ว

    I already liked the video before watching because it is the topic I had pinned on my todo list to research this week.
    Thanks Philipp

  • @adarshdev3810
    @adarshdev3810 3 ปีที่แล้ว

    It is awesome you explains concepts so simple. Hats off

  • @rahulchandrabhan
    @rahulchandrabhan 3 ปีที่แล้ว

    One of the best explanation. Simple and Easy. Great man

  • @telurasinmontasik2406
    @telurasinmontasik2406 ปีที่แล้ว

    Very clear explanation

  • @mdjahidulislam9205
    @mdjahidulislam9205 3 ปีที่แล้ว +1

    I was waiting for it.you just read my mind

  • @lyawileh.a8741
    @lyawileh.a8741 2 ปีที่แล้ว

    Thanks for useful explanation. 🙏

  • @tanishqsehgal2593
    @tanishqsehgal2593 2 ปีที่แล้ว

    Very Well Explained

  • @original_anu
    @original_anu 3 ปีที่แล้ว

    Simple, crisp explanations ❤

  • @tomaslopez9999
    @tomaslopez9999 2 ปีที่แล้ว

    Thanks for creating this video.

  • @anvarzaripboyev5730
    @anvarzaripboyev5730 ปีที่แล้ว

    Thanks bro, it is very usefull tutorial. Good luck!

  • @imagetag4618
    @imagetag4618 3 ปีที่แล้ว

    Wow. its so much clear understand. thank you. keep it up.

  • @Qrzychu92
    @Qrzychu92 3 ปีที่แล้ว

    I don't even programm in Kotlin (fellow dotnet dev here!), but this videos are very cool :) I'd love to have this tuff in C#

  • @leosvxy
    @leosvxy 3 ปีที่แล้ว

    thank you! , was so easy to understand your explanations

  • @bjugdbjk
    @bjugdbjk 3 ปีที่แล้ว

    Such a fab explanation, Thanks a ton, You should really do a lot more this kind of standalone videos of advanced concepts of Kotlin, it will b super helpful.

  • @theonlyarjun
    @theonlyarjun 3 ปีที่แล้ว

    just what i was searching for, awesome man

  • @DreckbobBratpfanne
    @DreckbobBratpfanne 2 ปีที่แล้ว +1

    i liked to use with eg in viewbinding, because then i can avoid typing the binding variable name xD, also you can use multiple of them within each other what is also pretty nifty

  • @TheMaciak90
    @TheMaciak90 3 ปีที่แล้ว

    Very well explained. Thank you mate.

  • @LittleWasteClub
    @LittleWasteClub 2 ปีที่แล้ว

    Thank you for the great explanation!

  • @marinbeslo7841
    @marinbeslo7841 3 ปีที่แล้ว

    your tutorials are just awesome!

  • @yuanyuechen2183
    @yuanyuechen2183 3 ปีที่แล้ว

    Thank you, I really learn a lot from you

  • @mahetachirag8395
    @mahetachirag8395 3 ปีที่แล้ว

    Your tutorial are awesome.

  • @zareahmer5949
    @zareahmer5949 3 ปีที่แล้ว

    You are Great man . Stay blessed

  • @indraj6669
    @indraj6669 3 ปีที่แล้ว

    Great explanation

  • @MyPhoneNotAccount
    @MyPhoneNotAccount ปีที่แล้ว

    so much better than the kotlin udacity explanations

  • @techmarinar
    @techmarinar 3 ปีที่แล้ว

    Thanx man this was very helpful 😊

  • @walkmn
    @walkmn 2 ปีที่แล้ว

    Good explanation for the latest one 😄

  • @yuripanov9670
    @yuripanov9670 3 ปีที่แล้ว

    You're the best! Thanks!

  • @aneessaleh2870
    @aneessaleh2870 3 ปีที่แล้ว +1

    amazing video mate , keep it up please

  • @vibinjoby
    @vibinjoby 2 ปีที่แล้ว

    Amazing just what i wanted after reading the docs and wondering where exactly all these get used

  • @abdelsttarahmed8821
    @abdelsttarahmed8821 3 ปีที่แล้ว

    you are legend man
    i wish seeing you doing a video on Providing offline capabilities with NetworkBoundResource

    • @PhilippLackner
      @PhilippLackner  3 ปีที่แล้ว

      I do that in my ktor course on pl-coding.com

  • @mehdiparsaei1867
    @mehdiparsaei1867 3 ปีที่แล้ว

    The best in the world! I am sure!

  • @koutheirelbehi4384
    @koutheirelbehi4384 3 ปีที่แล้ว

    This boi is on another level !

  • @Alvin-dg9pq
    @Alvin-dg9pq 3 ปีที่แล้ว

    Fantastic explanation, thanks a lot.

  • @tyrannus00
    @tyrannus00 ปีที่แล้ว

    Schönes Video, gut erklärt 👍

  • @ajaykumarvishwakarma4720
    @ajaykumarvishwakarma4720 3 ปีที่แล้ว

    Really informative video.
    Thanks a lot

  • @r.j.jeninjoseph4975
    @r.j.jeninjoseph4975 3 ปีที่แล้ว

    Really helpful thanks man

  • @themichael8767
    @themichael8767 ปีที่แล้ว

    Thank You very much, Sir!)

  • @ramakrishnavjoshi
    @ramakrishnavjoshi 3 ปีที่แล้ว

    Thanks for this video, Philipp. :)

  • @sckly43
    @sckly43 3 ปีที่แล้ว

    awesome video bru

  • @shidqi100
    @shidqi100 3 ปีที่แล้ว

    Great video expected man love your explanation, just asking will you make videos outside topic of android and mobile dev in general ex: Ai, web or desktop?

    • @PhilippLackner
      @PhilippLackner  3 ปีที่แล้ว +1

      Not planned right now, but I also don't say I never will. I might do some flutter videos in the next time

  • @aunghtayoo337
    @aunghtayoo337 2 ปีที่แล้ว

    Thanks. Philipp

  • @imnithish
    @imnithish 3 ปีที่แล้ว

    great vid as usual man

  • @rsobies
    @rsobies 2 ปีที่แล้ว

    in this case number is a property so it is a function. a function can return different results and that's why it is considered as mutuable. inside let block you are dealing directly with a field of the class. 'it' is a field, not a property. so it is immutable

  • @dreamer6228751
    @dreamer6228751 3 ปีที่แล้ว

    Thanks bro, it's excellent video !

  • @mohammedelamin1563
    @mohammedelamin1563 3 ปีที่แล้ว

    So helpful 😎

  • @RackaApps
    @RackaApps 3 ปีที่แล้ว

    Super helpful

  • @renaldyhiunarto4369
    @renaldyhiunarto4369 9 หลายเดือนก่อน

    coming from the future and seeing you with beard kinda weird lmao. Anyway, love all your vids

  • @scottbiggs8894
    @scottbiggs8894 ปีที่แล้ว

    I never realized before that Philipp looks like the Elvis operator ?: ;)

  • @VeronikaDoma-f5y
    @VeronikaDoma-f5y 2 ปีที่แล้ว

    Thank you!! 🤩

  • @alexnovikov1609
    @alexnovikov1609 ปีที่แล้ว

    Thank you!

  • @serhatcd
    @serhatcd 3 ปีที่แล้ว

    Thanks for video.. Also i see use function. What is the use function? This is also scoped function

  • @brucebane7401
    @brucebane7401 3 ปีที่แล้ว

    amazing tutorial!!!!!

  • @sudarshanh.s.521
    @sudarshanh.s.521 9 หลายเดือนก่อน

    this is awesome

  • @lional4898
    @lional4898 3 ปีที่แล้ว

    With is useful when using string builder. No need to create new variables. Simply call toString at the end

  • @hirarkiardipratamakiki8777
    @hirarkiardipratamakiki8777 3 ปีที่แล้ว

    i often use 'with 'to declare componen with viewbinding,such as onClickListener, and etc.

  • @mohamadalighezelbashyan4482
    @mohamadalighezelbashyan4482 2 ปีที่แล้ว

    Great!! thanks ❤

  • @vigneshdroid1654
    @vigneshdroid1654 ปีที่แล้ว

    Hi Bro, Your videos are very helpful for me. You Know Tamil language Bro?.

  • @himanshugoeldelhi
    @himanshugoeldelhi 2 ปีที่แล้ว

    Try executing that code if some people don't understand what 'let' does
    number?.let {
    number = null
    val num2 = it + 1
    }
    Here 'let' actually saved the global value of number and even in case number becomes null in 'let' block , still 'it' value will remain the same which you defined before

  • @abuiman5251
    @abuiman5251 3 ปีที่แล้ว

    Thanks!

  • @jayeshsuthar5590
    @jayeshsuthar5590 3 ปีที่แล้ว

    crystal clear....

  • @andriibibik39
    @andriibibik39 3 ปีที่แล้ว

    Very Good!

  • @mandroidx1474
    @mandroidx1474 3 ปีที่แล้ว

    Appreciated
    Keep it up❤

  • @cinemovieone
    @cinemovieone 3 ปีที่แล้ว

    O melhor, muito bom os vídeos.

  • @MrLukasw44
    @MrLukasw44 ปีที่แล้ว

    but is that mean that apply is thread safe? so if we got an object and we would like to run 3 methods on that object and that object is a global variable so the question is if we use apply those 3 operations will be thread safe?

  • @ankitpareek8979
    @ankitpareek8979 2 ปีที่แล้ว

    I have an usecase where i need to call a suspend function(some network call) on completion of it i need to perform other local task. I am trying to use also there but getting unexpected results. Can u explain this !!!

  • @shreyashachoudhary480
    @shreyashachoudhary480 ปีที่แล้ว

    Great!

  • @soontaek
    @soontaek 3 ปีที่แล้ว

    I left you some feedback on your previous video (Preference Datastore)

  • @fourcade7851
    @fourcade7851 3 ปีที่แล้ว

    Thank You Bro

  • @pratikchauhan989
    @pratikchauhan989 3 ปีที่แล้ว

    Good one 👏👏

  • @coffeedude
    @coffeedude 2 ปีที่แล้ว

    I learnt that you can use 'also' to swap variables but I don't understand why it works. ie:
    var foo = "foo"
    var bar = "bar"
    foo = bar.also { bar = foo }
    print(foo + bar) // barfoo

  • @MetehanBOLAT
    @MetehanBOLAT 2 ปีที่แล้ว

    You said "run" is the same as "with", but "with" is almost the same as "apply" isn't it? Because "with" and "apply" return expected types. run does not return. also {
    Thank you very much, it was a very informative video by the way.
    } :)

  • @shreyashchoudhary7413
    @shreyashchoudhary7413 3 ปีที่แล้ว

    Epic!

  • @riyupapa39
    @riyupapa39 2 ปีที่แล้ว

    I usually use run like this
    var a:String?=null
    //When i want do something if only a is not null,
    a?.run{
    //todo
    }

  • @tejas5331
    @tejas5331 3 ปีที่แล้ว

    Hey can you tell me how to fetch data about places from API and show on Map in Android?

  • @jasperjohnsoncalapini
    @jasperjohnsoncalapini 3 ปีที่แล้ว

    Can I do something else inside 'apply' and 'run' for example
    textbox.run {
    //is this a good practice
    val x = 10
    val squareX = x*x
    // or maybe some loops
    text = "$x"
    }
    and one more thing, would you prefer 'apply' over 'run' when when modifying views/components in android?
    Thanks

    • @PhilippLackner
      @PhilippLackner  3 ปีที่แล้ว +1

      no, I would move out of these functions whatever you can move out, this doesn't look very readable to me. If you just modify views and don't care about the return value it doesn't matter if you use apply or run

  • @DeepSingh-wk5ts
    @DeepSingh-wk5ts 3 ปีที่แล้ว

    Thanks

  • @nishantpattani3053
    @nishantpattani3053 3 ปีที่แล้ว

    what is difference between return@let and simple return in let function

  • @abhikhandelwal8253
    @abhikhandelwal8253 3 ปีที่แล้ว

    All these Scope Functions are not showing in android studio ? it gives me an error and said make a external function for using these function why?