Hi all :android-wave: , I use `combine` like this,...
# detekt
a
Hi all 👋 , I use
combine
like this,
Copy code
public val screenUIData: StateFlow<MyResult<AddOrEditAccountScreenUIData>?> = combine(
        flow1,
        flow2,
        flow3,
        flow4,
        flow5,
        flow6,
        ...
    ) { flows ->
        val data1 = flows[0] as? <DataType> ?: <DefaultValue>
        val data2 = flows[1] as? <DataType> ?: <DefaultValue>
        val data3 = flows[2] as? <DataType> ?: <DefaultValue>
        val data4 = flows[3] as? <DataType> ?: <DefaultValue>
        val data5 = flows[4] as? <DataType> ?: <DefaultValue>
        val data6 = flows[5] as? <DataType> ?: <DefaultValue>
        ...
    }
Detekt is throwing error
MagicNumber
for the index usage of the
flows
. How to ignore this across the project? Note: I don't want to completely disable the
MagicNumber
lint check, I want to ignore only for this particular use case. Can anyone please help thank you color
e
Copy code
combine(...) { (data1, data2, data3, data4, data5) ->
    val data1 = data1 as? <DataType> ?: <DefaultValue>
    ...
a
I have to
combine
more than 5 flows.
d
@Abhimanyu for cases like that where the meaning of the number is obvious, can you suppress the issue? https://detekt.dev/docs/introduction/suppressing-rules
👌 1
Also, ephemient's suggestion seems like a better way to achieve what you want. Is there a reason it doesn't work?
e
there are
combine
overloads for 2 to 5 flows, whose lambdas receive multiple parameters and thus would be written
Copy code
combine(flow1, flow2, flow3) { data1, data2, data3 ->
as well as
combine
overloads for
vararg Flow
and
Iterable<Flow>
, whose lambdas receive an array. the array can be destructured immediately as I wrote above.
a
Yes, but the
combine
has only 2 to 5 parameter overloads.
e
my point is that the varargs overload works fine with destructuring
a
I am getting this error 🤔
Copy code
Destructuring declaration initializer of type Array<Any?> must have a 'component6()' function
c
@Abhimanyu check this out. If you do something like this you can suppress it in this file only. https://github.com/chrisbanes/tivi/blob/main/core/base/src/commonMain/kotlin/app/tivi/extensions/Combine.kt
a
Okay, that looks like a better approach. thank you color
e
component6
can be defined yourself, as in the previous playground link
but yeah if you're often doing huge heterogenous `combine`s them making your own strongly type wrappers is good