diesieben07
12/29/2018, 3:09 PMval x: MutableCollection<String> get() = TODO()
fun main() {
println(MutableCollection::class) // prints "class kotlin.collections.Collection
println(Set::class.isSubclassOf(MutableCollection::class)) // prints true, should be false
println(::foo.returnType) // prints kotlin.collections.MutableCollection<kotlin.String>, yay!
println(::foo.returnType.classifier) // prints kotlin.collections.Collection, :(
}
Is there any way to figure out if a KType represents a mutable collection type?gildor
12/30/2018, 10:43 AMdiesieben07
12/30/2018, 10:10 PMudalov
KType.toString
right now. Therefore, if you have a KType, you can use a dirty workaround of type.toString().startsWith("kotlin.collections.Mutable")
for now :). If you only have a value of a Kotlin collection, you can use kotlin.jvm.internal.TypeIntrinsics.isMutableCollection
and others to check if its class extends MutableCollection
(these methods return true for all foreign (Java) collections, even if they're known to be readonly otherwise)
We have plans to make different KClass instances for mutable vs readonly collections: https://youtrack.jetbrains.com/issue/KT-11754diesieben07
01/02/2019, 2:42 PM