zmunm
08/05/2020, 3:56 AMObservable.something()
.map {
mutableList.apply {
addAll(list)
}
}
Console says UnnecessaryApply
.. But I need return MutableList
I don't know if this is really wrong. How can I fix it? Thank you for reading..nkiesel
08/05/2020, 4:38 AMObservable.something().map { mutableList.addAll(list) }
not be enoughzmunm
08/05/2020, 4:46 AMgammax
08/05/2020, 5:06 AM.also{}
will do the same job, will not be flagged and will also be a better fit here imho as you’re having a side effect.zmunm
08/05/2020, 5:21 AMapply
is more correct it this case.
How about this case
Observable.something()
.map {
Person("Adam").apply {
age = 32
city = "London"
}
}
Is there any flag? I can't found about that..gammax
08/05/2020, 5:22 AMIs there any flag?What do you mean?
gammax
08/05/2020, 5:23 AMUnnecessaryApply
is designed to flag if you use apply for only one statement. Imho .apply{}
is useful when you’re configuring an instance and you want to mutate several of its fields.
Your case instead is applying a side effect on your mutableList
and returning it.zmunm
08/05/2020, 5:37 AMIs there any flag?
mean
Is there any difference inDo you mean thatexceptalso, apply
it, this
addAll
is a function, which can cause side effect? It understood.
I also have second case like Person().apply{ age = 32 }
It would be happy if age
went into the constructor, but it is difficult to do so.
Is it better to use aslo if there is only one line even for field set?gammax
08/05/2020, 5:42 AMIs there any difference inNo there are no other difference other the one you mentioned.exceptalso, apply
it, this
Brais Gabin
08/07/2020, 11:33 AMBrais Gabin
08/07/2020, 11:35 AMgammax
08/07/2020, 12:18 PMI don’t like this code that much because you should not use mutable objects in a stream but that’s out of the scope of detekt and the issue.Agree. That’s also the root cause of the problem actually. Ideally RxJava would have an operator like
onEach
or similarBrais Gabin
08/07/2020, 12:19 PMdoOnNext
Brais Gabin
08/07/2020, 12:19 PMgammax
08/07/2020, 12:22 PMObservable.something()
.doOnEach {
mutableList.addAll(it)
}
Brais Gabin
08/07/2020, 1:03 PM