Did You Know About THIS Kotlin Feature? đŸ€Ż

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 26. 08. 2024
  • Follow for more Android & Kotlin tips 🙌

Komentáƙe • 31

  • @asuras2891
    @asuras2891 Pƙed rokem +18

    Its quite useful when dealing with recursive functions as you can have one function which accepts the start parameters and have an inner function for the actual recursive call with an accumulator initialized. Also works great when writing tailrec functions

    • @-karter-4556
      @-karter-4556 Pƙed rokem

      Probably the only use case I can get behind

  • @ryszardmachmudow9175
    @ryszardmachmudow9175 Pƙed rokem +3

    Useful in Composable functions, when you do not want to pass many parameters to nested function

  • @ChrisAthanas
    @ChrisAthanas Pƙed rokem +2

    I like to use nested functions for helper functions that break up proceases without having to use comments.

  • @mohegyux4072
    @mohegyux4072 Pƙed rokem

    I use a lot, even if not repetitive code, like if a function does multiple tasks, and each task is a big chunck of code, then I move each chunck into an inner function, makes the code readable for me

  • @dies200
    @dies200 Pƙed rokem +1

    It's a pretty common pattern in compose

  • @camilohernandezruiz2776
    @camilohernandezruiz2776 Pƙed 10 měsĂ­ci

    I use them to clean the code in large functions. If anything goes wrong, the stack trace tells me the name of the inner function.

  • @mrjackson9137
    @mrjackson9137 Pƙed rokem +1

    that's mindblowing!

  • @iamwhoiam798
    @iamwhoiam798 Pƙed rokem

    It’s good for a cleaner code but you didn’t mention whether nested function able to access outer function’s local variables

  • @tatavarthitarun2739
    @tatavarthitarun2739 Pƙed rokem +3

    Interesting but still it looks wrong to me 😅, may be we need to get used to it

  • @darwinspace
    @darwinspace Pƙed rokem +1

    what is better, lambas inside a function or nested functions?

  • @oubihinoureddine9025
    @oubihinoureddine9025 Pƙed rokem

    Nice trick, I once had a scenario where this could be useful and was thinking if it's possible 😂

  • @antpr2
    @antpr2 Pƙed rokem +1

    You could also put it in a lamda.
    I.e:
    val addNumbers: (Int, Int) -> Int = { x, y -> x + y }
    val result = addNumbers(2, 3) // result = 5
    Looks cleaner imo

  • @vinayrajagopal5478
    @vinayrajagopal5478 Pƙed rokem

    It's also quite useful to do recursive actions without modifying the outer function's parameters.
    I also like to use it when I need to do a "helper" method that does the bulk of the work, we can avoid passing arguments from the outerscope.

  • @azamovdev
    @azamovdev Pƙed rokem

    oh nice cool. ishlarizga omad . hamma video va contentlaringiz ajoyib va foydali.

  • @franciscobneto
    @franciscobneto Pƙed 9 měsĂ­ci

    hi guys, in this case, why not use a lambda func?

  • @saifahmed7720
    @saifahmed7720 Pƙed rokem

    what about lamda functions i use it in this scenario instead of that normal fuction
    is there is a diff ?

  • @yz2778
    @yz2778 Pƙed 8 měsĂ­ci

    is that another way of factory pattern

  • @Rin-qj7zt
    @Rin-qj7zt Pƙed rokem

    too lazy to cut the last bit of this video off? lol same

  • @bogdan.801
    @bogdan.801 Pƙed rokem

    I just use lamda variable in this case

  • @QuantumQabantum
    @QuantumQabantum Pƙed rokem

    Really useful feature for me.

  • @engine_man
    @engine_man Pƙed rokem

    Should have cut out the last second 😅

  • @DJOrangeJoe
    @DJOrangeJoe Pƙed rokem +2

    Kotlin is the best language.

    • @mrjackson9137
      @mrjackson9137 Pƙed rokem

      think that only Go is better đŸ€Œ

    • @DJOrangeJoe
      @DJOrangeJoe Pƙed rokem

      @@mrjackson9137 I dont know go but I really love kotlin.

  • @ncba
    @ncba Pƙed rokem

    I still do not see the use of it being a nested function. Need to understand it better.

    • @megaleuzao
      @megaleuzao Pƙed rokem +1

      maybe when you need to execute repetitive code over a val declared inside the function itself i dunno

    • @jacobmccready6528
      @jacobmccready6528 Pƙed rokem +1

      I imagine it'd be useful when you have some value in the scope of the parent function that you either don't want to calculate again in a regular class-level function or you simply can't because no other class-level functions would have access to that data. I've personally run into situations where this might've been useful but I tend to just use lambdas instead.

    • @ncba
      @ncba Pƙed rokem

      @@megaleuzao let me try write an example of this

  • @safionweb
    @safionweb Pƙed rokem

    Hmmm