Open AlecKazakova opened 6 years ago
This might be a problem with Kotlin itself.
assertThat(MutableList::class.qualifiedName).isEqualTo("kotlin.collections.MutableList")
org.junit.ComparisonFailure:
Expected :kotlin.collections.MutableList
Actual :kotlin.collections.List
... looks like MutableList
is completely erased at compile-time.
assertThat(MutableList::class).isEqualTo(List::class)
should this be something kotlinpoet addresses then? List::class.asMutableTypeName()
or better to just leave it as the manual ClassName
usage
given your findings its probably impossible to have something thats safe at compile time
asMutableTypeName()
is an interesting idea!
Yeah something like...
fun KClass<*>.asMutableTypeName(): ClassName {
return when (this) {
List::class -> MUTABLE_LIST
Map::class -> MUTABLE_MAP
Set::class -> MUTABLE_SET
else -> asTypeName()
}
}
...is handy
@afollestad btw we've introduced a large number of constants for frequently used ClassName
s:
fun KClass<*>.asMutableTypeName(): ClassName {
return when (this) {
List::class -> MUTABLE_LIST
Map::class -> MUTABLE_MAP
Set::class -> MUTABLE_SET
else -> asTypeName()
}
}
This, annoyingly, isn't covered by the new typeOf()
API either :/
@Test
fun typeNameOf_mutable() {
val type = typeNameOf<MutableList<String>>()
assertThat(type.toString()).isEqualTo("kotlin.collections.MutableList<kotlin.String>")
}
yields
expected: kotlin.collections.MutableList<kotlin.String>
but was : kotlin.collections.List<kotlin.String>
This is filed now https://youtrack.jetbrains.com/issue/KT-35877
facing the same problem, no way to create a property with MutableList type, please help
facing the same problem, no way to create a property with MutableList type, please help
Did you actually read the issue?
I have worked around with ClassName("kotlin.collections", "MutableList")
If I do
the generated code uses
kotlin.collections.List
I have worked around with
ClassName("kotlin.collections", "MutableList")