0

In which use cases does one use the other similar scope functions instead of let, like run or also, or even with?

Amit Dash
  • 584
  • 8
  • 21
  • There's this nice table in the [documentation](https://kotlinlang.org/docs/scope-functions.html) that you can easily find on Google. What don't you understand from there? – Sweeper Jun 14 '21 at 02:24
  • @Sweeper thanks for the link, though I have made the question more focused now and I hope specific context from the link can be used here to answer it – Amit Dash Jun 14 '21 at 03:46
  • There are about 10000 blog posts about this on Medium. Seems like every other newcomer to Kotlin wants to rewrite that same page from the documentation. Anyway, your update to the question doesn't help. That documentation page linked above gives examples of how each of them should be used. What about that list do you not understand specifically? All the scope functions are similar to each other in different ways. – Tenfour04 Jun 14 '21 at 03:59
  • 2
    @Tenfour04 This is exactly what is confusing - _All the scope functions are similar to each other in different ways_. Also this question is specifically about alternatives to `let` for general use cases like executing some logic on non-null objects and such. Finally IMO, the new-comers might find this info useful. – Amit Dash Jun 14 '21 at 04:32
  • 1
    See also [Example of when should we use run, let, apply, also and with on Kotlin](https://stackoverflow.com/questions/45977011/example-of-when-should-we-use-run-let-apply-also-and-with-on-kotlin). – Adam Millerchip Jun 14 '21 at 04:45

1 Answers1

2

There is no right answer. It's a judgement call. Choose the one that best fits your situation whether you want this or it available internally, and whether you want to return the lambda result or the value of the object you called the scope function on.

Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
  • Thanks for the answer Adam. I think both the points bring clarity: **1.** _whether you want `this` or `it` available internally_ **2.** _whether you want to return the lambda result or the value of the object_ – Amit Dash Jun 14 '21 at 04:42