# Bug in Kotlin reflection

**URL:** <https://discuss.kotlinlang.org/t/bug-in-kotlin-reflection/18843>\
**Category:** Support\
**Created:** [August 19, 2020, 10:11pm UTC](https://discuss.kotlinlang.org/t/bug-in-kotlin-reflection/18843 "2020-08-19T22:11:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vk](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@vk](https://discuss.kotlinlang.org/u/vk)\
**Post date:** [August 19, 2020, 10:11pm UTC](https://discuss.kotlinlang.org/t/bug-in-kotlin-reflection/18843/1 "2020-08-19T22:11:14Z")

</div>

It looks like there is a bug in Kotlin reflection.

I am getting List instead of MutableList for property:

`val assets: MutableList<AssetDto> = ArrayList()`

– for this declaration I am getting List instead of Mutable List via  
clazz: KClass\<\*\>  
for (prop in clazz.declaredMemberProperties) {  
val c = p.returnType.classifier //\<-- **It gives me kotlin.collections.List instead of MutableList**!

}

Is it a bug or what?

P.S. I am now on Kotlin 1.4-M1 - should I upgrade to 1.4 release?

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [August 19, 2020, 11:48pm UTC](https://discuss.kotlinlang.org/t/bug-in-kotlin-reflection/18843/2 "2020-08-19T23:48:43Z")

</div>

> <https://stackoverflow.com/questions/52580173/listof-returns-mutablelist/52591274#52591274>

> [@yole](#):
>
> The separation between `List` and `MutableList` is an illusion created by the Kotlin compiler. At runtime, Kotlin uses Java collection classes, which only have a single `List` interface containing both read and mutation methods. Compile-time references to `List` and `MutableList` are both compiled to `java.util.List` references. Therefore, **it’s not possible to detect whether a list is a `MutableList` at runtime**.

---

<div class="post-metadata">

**Author:** ![vk](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@vk](https://discuss.kotlinlang.org/u/vk)\
**Post date:** [August 20, 2020, 6:51pm UTC](https://discuss.kotlinlang.org/t/bug-in-kotlin-reflection/18843/3 "2020-08-20T18:51:53Z")

</div>

Thanks for information. And yes, **It is a bug**. – Property type should be available via reflection exactly as it is declared.

I hope Kotlin team will take care of fixing it.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [August 20, 2020, 10:55pm UTC](https://discuss.kotlinlang.org/t/bug-in-kotlin-reflection/18843/4 "2020-08-20T22:55:34Z")

</div>

Well no, it’s not a bug. The limitation is intrinsic in the artificial way of separating a mutable collecting from an immutable one when stuff was already existing.

If you need a really immutable collection go for [kotlinx.collections.immutable](https://github.com/Kotlin/kotlinx.collections.immutable).

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [August 21, 2020, 12:10am UTC](https://discuss.kotlinlang.org/t/bug-in-kotlin-reflection/18843/5 "2020-08-21T00:10:10Z")

</div>

> [@lamba92](#):
>
> mutable collecting from an immutable

mutable from **readonly**. I know this might seem like a small difference but it’s the cause for many bugs and easily confuses newcomers to kotlin. Kotlin’s collections are readonly not immutable.

As you correctly pointed out for immutable collections you should use the library linked above.

* * *

Is it a bug? Maybe. There is an argument to be made that kotlin only has mutable/readonly during compile time and that this is just the limitation of the runtime. We would expect `returnType.javaType` to be the same for `List` and `MutableList`. However this is part of the kotlin reflection (not just java reflection) and we actually have a `KType` which should know the difference between them.  
This information is available through the kotlin meatdata annotation so we can expect reflection to know about it.
