# Kotlin `combine()` Destructuring Beyond 5 Flows

## The Problem

`kotlinx.coroutines.flow.combine` has overloads for 2–5 flows that return `Pair`/`Triple`/etc., plus a varargs overload that returns `Array<T>`. When you use `combine(flow1, flow2, ..., flowN)` with N > 5, the result type is `Array<T>`. Kotlin only provides `component1()`–`component5()` extension functions on arrays, so destructuring syntax fails:

```kotlin
combine(f1, f2, f3, f4, f5, f6, f7) { args ->
    val (a, b, c, d, e, f, g) = args  // ERROR: no component6(), component7()
}
```

## The Fix

Replace destructuring with indexed access + explicit type casting:

```kotlin
combine(
    preferencesManager.enableAutoPlay,
    preferencesManager.subtitleLanguage,
    preferencesManager.subtitleSize,
    preferencesManager.theme,
    preferencesManager.preferredQuality,
    preferencesManager.autoplayPreviews,
    preferencesManager.resumeBehavior,
    preferencesManager.skipIntroMode,
    // ... up to 65+ flows
) { args ->
    val autoplayNext = args[0] as? Boolean ?: false
    val subtitleLang = args[1] as? String ?: "English"
    val subtitleSizeVal = (args[2] as? Number)?.toInt() ?: 24
    val theme = args[3] as? String ?: "system"
    val quality = args[4] as? String ?: "1080p"
    val prevs = args[5] as? Boolean ?: false
    val resume = args[6] as? String ?: "ask_me"
    val intro = args[7] as? String ?: "show_button"
    // ... continue for all flows
}
```

## Key Rules

1. **Count the flows** — the index must match the `combine()` argument order exactly.
2. **Cast correctly** — check the target data class to get the right type:
   - `Set<String>` → `(args[N] as? Set<*>)?.filterIsInstance<String>()?.toSet() ?: emptySet()`
   - `List<String>` → `(args[N] as? List<*>)?.filterIsInstance<String>() ?: emptyList()`
   - `Int` → `(args[N] as? Number)?.toInt() ?: 0`
   - `String` → `args[N] as? String ?: ""`
   - `Boolean` → `args[N] as? Boolean ?: false`
3. **Don't use `as` (non-null)** — use `as?` (safe cast) with `?:` default, since flows emit nullable values during type erasure.
4. **Keep an index map** — when editing, maintain a comment with the variable-to-index mapping to prevent off-by-one errors.

## Source

Discovered when a 65-flow `combine()` failed with 65 `"operator function 'componentN()'"` errors — one per destructured variable beyond component5.
