-
Notifications
You must be signed in to change notification settings - Fork 14
Using scope functions apply/with/run/also/let #35
Comments
I use
On the other hand, I use
|
@cypressious Why you prefer |
I use var text: String? = null
fun showText() {
text?.let { text ->
println(text)
}
} I often see |
@MarcinMoskala it's just to differentiate. I use |
Another good use of with(activity as MyActivity) {
myActivitymethod()
...
} |
Using
|
I was trying out what to use when in the same year when this issue was created. My understanding back then is that these functions should be used based on intention:
|
apply
Use
apply
for initialization:also
Use
also
overapply
if the receiver is used for anything other than setting properties or function calls on it:Prefer
also
overapply
if there already are multiple receivers in scope, especially if you make calls on any outer receivers:apply/with/run
Prefer
apply
/run
overwith
if the receiver is nullable.Prefer
run
/with
overapply
if the returned value is not usedChoose one of the above and use it consistently.
let
Prefer
let
overrun
in method chains that transform the receiverThe text was updated successfully, but these errors were encountered: