# Data class get all components/properties

**URL:** <https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409>\
**Category:** Language Design\
**Created:** [August 8, 2022, 6:37pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409 "2022-08-08T18:37:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 8, 2022, 6:37pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/1 "2022-08-08T18:37:41Z")

</div>

Hello, I suppose, this question was raised but how can I get all properties of a `data class`? It is possible to get a `componentN` but what’s the purpose if I can’t get them all in a loop and even don’t know the `N`? I need to make `.forEach` for all the properties and currently to do it better than use reflection, I rewrote the whole class to a hash map.

---

<div class="post-metadata">

**Author:** ![Master-Leodin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/master-leodin/32/9455_2.png) [@Master-Leodin](https://discuss.kotlinlang.org/u/Master-Leodin)\
**Post date:** [August 8, 2022, 9:03pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/2 "2022-08-08T21:03:40Z")

</div>

If you put the code here maybe help on help.  
Just the data class part and what you can do with.

---

<div class="post-metadata">

**Author:** ![ebrowne72](https://avatars.discourse-cdn.com/v4/letter/e/85f322/32.png) [@ebrowne72](https://discuss.kotlinlang.org/u/ebrowne72)\
**Post date:** [August 8, 2022, 9:27pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/3 "2022-08-08T21:27:35Z")

</div>

The `componentN` functions are there to support [destructuring](https://kotlinlang.org/docs/destructuring-declarations.html). If you want to get all of the properties of some random data class, that’s what reflection is for.

Question: why doesn’t your code know what data class it’s working with?

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 9, 2022, 7:01am UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/4 "2022-08-09T07:01:29Z")

</div>

```auto
data class some (val a: Int = 0, val b: Int = 2, val c:Int = 3){
    fun getAll() = this.components.forEach{ something }
}

```

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 9, 2022, 7:02am UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/5 "2022-08-09T07:02:49Z")

</div>

The data class has above 30 parameters, other way I need to repeat their names every time.

---

<div class="post-metadata">

**Author:** ![alexis.manin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/alexis.manin/32/8381_2.png) [@alexis.manin](https://discuss.kotlinlang.org/u/alexis.manin)\
**Post date:** [August 9, 2022, 8:08am UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/6 "2022-08-09T08:08:32Z")

</div>

The problem is that each property of a data class could have a different type.

If implemented in a generic fashion, the loop action would only be able to consume `Any?` values, and therefore it would be something like this :

```nohighlight
    fun forEachProperty(action: (Pair<String, Any?>) -> Unit);

```

That looks hardly usable / efficient for me.

Now, if you’re in the particular case where all your properties have the same type, I don’t think that any convenient solution exists yet.

You could create a compiler plugin or something similar that creates a `forEach` extension function matching your case.

I see another workaround that might provide you with the forEach. But in the same time, it will cause you to lose almost all other useful tools of dataclass (copy method, equality method, etc.) : you can create a class decorating a list or an array, and then associate a property to each index :

```nohighlight
class IntValueForm(private val values: IntArray) {
    companion object { val PROPERTY_COUNT : Int = 3 }
    init {
        require(values.size == PROPERTY_COUNT)
    }

    val a : Int get() = values[0]
    val b: Int get() = values[1]
    val c: Int get() = values[2]

    fun forEach(action: (Int) -> Unit) = values.forEach(action)
}

```

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 9, 2022, 9:25am UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/7 "2022-08-09T09:25:26Z")

</div>

So, if your class has 100+ properties, you’ll rewrite them all? Sorry, but it’s not usable in modern world. And there can be a lot of different data classes with different amount of properties.

---

<div class="post-metadata">

**Author:** ![akurczak](https://avatars.discourse-cdn.com/v4/letter/a/f07891/32.png) [@akurczak](https://discuss.kotlinlang.org/u/akurczak)\
**Post date:** [August 9, 2022, 11:25am UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/8 "2022-08-09T11:25:34Z")

</div>

One may argue that having 100+ properties in a single class is not usable at all, no matter the language.

Could you write something more about your specific use case? Why not just use a collection in the first place, if you need to iterate over the values?

---

<div class="post-metadata">

**Author:** ![jedidia](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@jedidia](https://discuss.kotlinlang.org/u/jedidia)\
**Post date:** [August 11, 2022, 6:38am UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/9 "2022-08-11T06:38:27Z")

</div>

I cannot for the life of me imagine a use case for a data class with 100+ properties. It sounds like having a database with only one table but hundreds of columns… I.e. something in the design of your data structures has gone seriously wrong.

Depending on your use case, you should either pick that thing apart into multiple classes so you can actually keep separate data separate and understandable, _or_ you are trying to use a data class when you should be using a map (if all properties in your data class have the same type, it’s almost definitely the later).

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 11, 2022, 3:25pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/10 "2022-08-11T15:25:13Z")

</div>

You can try to gen any external JSON response, like movie info or other stuff. There are a lot of parameters to serialize, e.g.

---

<div class="post-metadata">

**Author:** ![donaldtam](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@donaldtam](https://discuss.kotlinlang.org/u/donaldtam)\
**Post date:** [August 12, 2022, 1:25pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/11 "2022-08-12T13:25:19Z")

</div>

Did you even read the previous comment?  
How would you loop through the properties? These properties are likely of different types, and looping via index is done at runtime, the best you can get is looping through the properties and consume them as Any?, which, as mentioned, isn’t very usable. You will have to either use reflection if you really need to loop through the fields.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 12, 2022, 4:40pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/12 "2022-08-12T16:40:50Z")

</div>

Ok, currently I have about 30+ parameters same type for a single data class. And I need to proceed different data classes. Think to use map for them.

---

<div class="post-metadata">

**Author:** ![donaldtam](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@donaldtam](https://discuss.kotlinlang.org/u/donaldtam)\
**Post date:** [August 12, 2022, 4:53pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/13 "2022-08-12T16:53:00Z")

</div>

If you have 30+ parameters with the same type, you probably should use an IntArray or a List?

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 13, 2022, 4:26pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/14 "2022-08-13T16:26:37Z")

</div>

I don’t find a proper way to store parameters names and other data in a List better than in a map.

---

<div class="post-metadata">

**Author:** ![piacenti](https://avatars.discourse-cdn.com/v4/letter/p/f475e1/32.png) [@piacenti](https://discuss.kotlinlang.org/u/piacenti)\
**Post date:** [August 13, 2022, 5:27pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/15 "2022-08-13T17:27:09Z")

</div>

Wow in that case don’t use a data class at all. Just use a generic JSON type object. That way you at least know you will get a more limited number of basic JSON types (maps, arrays, ints, strings etc.). Many JSON libraries provide that for you Not sure how genetically looping that would be useful but it would accomplish what you need without having to account for some crazy number of types. If you still need data classes for more explicit field access you could still do that but that wouldn’t be your choice for looping. Two separate flows there depending on need.

---

<div class="post-metadata">

**Author:** ![deviant](https://avatars.discourse-cdn.com/v4/letter/d/f6c823/32.png) [@deviant](https://discuss.kotlinlang.org/u/deviant)\
**Post date:** [August 15, 2022, 8:24am UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/16 "2022-08-15T08:24:27Z")

</div>

reflection is all you need

---

<div class="post-metadata">

**Author:** ![Beholder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/beholder/32/2078_2.png) [@Beholder](https://discuss.kotlinlang.org/u/Beholder)\
**Post date:** [August 18, 2022, 12:11pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/17 "2022-08-18T12:11:49Z")

</div>

Hash map may be indeed the better solution.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 18, 2022, 1:29pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/18 "2022-08-18T13:29:37Z")

</div>

yes, but its parameters are unstable/unknown.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [August 18, 2022, 7:11pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/19 "2022-08-18T19:11:48Z")

</div>

I agree with others that it seems you’re trying to do something strange. You should almost never need a class with so many properties. But if you really want to do this then Kotlin provides an utility that fits nicely between a map and an object with properties. See this example:

```kotlin
fun main() {
    val foo = MyClass()
    foo.prop1 = "value1"
    foo.prop2 = "value2"
    foo.prop3 = "value3"
    
    // {prop1=value1, prop2=value2, prop3=value3}
    println(foo.properties)
}

class MyClass private constructor(val properties: MutableMap<String, String>) {
    constructor() : this(mutableMapOf())

    var prop1 by properties
    var prop2 by properties
    var prop3 by properties
}

```

It has worse performance than regular properties, because it stores props in a map. But it lets you easily list all properties, iterate over them, etc.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [August 20, 2022, 6:54pm UTC](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409/20 "2022-08-20T18:54:08Z")

</div>

Yeah, but looks better to add `components.all` to the `data class`.

[Next page](https://discuss.kotlinlang.org/t/data-class-get-all-components-properties/25409.md?page=2)
