# Destructuring multiple components

**URL:** <https://discuss.kotlinlang.org/t/destructuring-multiple-components/3355>\
**Category:** Language Design\
**Created:** [June 16, 2017, 11:21am UTC](https://discuss.kotlinlang.org/t/destructuring-multiple-components/3355 "2017-06-16T11:21:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tieskedh](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/tieskedh/32/2816_2.png) [@tieskedh](https://discuss.kotlinlang.org/u/tieskedh)\
**Post date:** [June 16, 2017, 11:21am UTC](https://discuss.kotlinlang.org/t/destructuring-multiple-components/3355/1 "2017-06-16T11:21:09Z")

</div>

Just a suggestion.

On this moment,we destruct by calling the functions componentN() where N appoints to the component we need.

```
class Test(val a: A, val b: B, val c:C){
    operator fun component1() = a.first.toUpperCase()
    operator fun component2() = b.first.toUpperCase()
    operator fun component3() = c.first.toUpperCase()
    operator fun component4() = a.second % 5 + 1
    operator fun component5() = b.second % 5 + 1
    operator fun component6() = c.second % 5 + 1
}

```

> **my current solution**
>
> On this moment, to make the above easier, I’m using an interface and its implementation to make it more clear:
> 
> ```
> interface Destruct6<out A, out B, out C, out D, out E, out F> {
> operator fun component1() : A
> operator fun component2() : B
> operator fun component3() : C
> operator fun component4() : D
> operator fun component5() : E
> operator fun component6() : F
> }
> class Destruct6Impl<out A, out B, out C, out D, out E, out F>(
> private val comp1: ()->A,
> private val comp2: ()->B,
> private val comp3: ()->C,
> private val comp4: ()->D,
> private val comp5: ()->E,
> private val comp6: ()->F
> ) : Destruct6<A, B, C, D, E, F> {
> override operator fun component1(): A = comp1()
> override operator fun component2(): B = comp2()
> override operator fun component3(): C = comp3()
> override operator fun component4(): D = comp4()
> override operator fun component5(): E = comp5()
> override operator fun component6(): F = comp6()
> }
> 
> ```
> 
> Now I can write the above class as:
> 
> ```
> class Test(val a: A, val b: B, val c:C)
> :Destruct6<String,String,String,Int,Int,Int> 
> by Destruct6Impl(
> {a.first.toUppercase()},
> {b.first.toUppercase()},
> {c.first.toUppercase()},
> {a.second % 5 + 1},
> {b.second % 5 + 1},
> {c.second % 5 + 1}
> )
> 
> ```

To make it more clear, I would like to have a function for destructing, where you can return a dataclass:

```
class Test(val a: A, val b: B, val c:C){
    operator fun components() = Destruct6(
         a.first.toUppercase(),
         b.first.toUppercase(),
         c.first.toUppercase(),
         a.second % 5 + 1,
         b.second % 5 + 1,
         c.second % 5 + 1
    )
}

```

The adventage over the seperated components is:

- Removed boilerplate, which makes it clearer
- When Destruct6 is a data class, maybe in the future the destruction names can be inferred (just like variables in constructors)?
- By calling a function for creating Destruct6, the representation of the destruction variables can easily be delegated.

The advantage over my solution is:

- You don’t need the interface anymore (was needed for not having Destruct6 as parent)
- In my solution, everyting has to be known at class declaration [without workaround](https://discuss.kotlinlang.org/t/class-delegate-using-internal-property/1867).

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [June 19, 2017, 7:13am UTC](https://discuss.kotlinlang.org/t/destructuring-multiple-components/3355/2 "2017-06-19T07:13:20Z")

</div>

Can you please describe a real scenario where you need to manually implement multiple componentN() functions in such a way?
