Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using kotlin mutable collections for typenames #279

Open
AlecKazakova opened this issue Nov 10, 2017 · 10 comments
Open

Using kotlin mutable collections for typenames #279

AlecKazakova opened this issue Nov 10, 2017 · 10 comments
Milestone

Comments

@AlecKazakova
Copy link
Member

If I do

MutableList::class.asTypeName()

the generated code uses kotlin.collections.List

I have worked around with ClassName("kotlin.collections", "MutableList")

@swankjesse
Copy link
Collaborator

swankjesse commented Nov 10, 2017

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

@swankjesse
Copy link
Collaborator

... looks like MutableList is completely erased at compile-time.

assertThat(MutableList::class).isEqualTo(List::class)

@AlecKazakova
Copy link
Member Author

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

@swankjesse
Copy link
Collaborator

asMutableTypeName() is an interesting idea!

@afollestad
Copy link
Member

afollestad commented Jul 16, 2019

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

@Egorand
Copy link
Collaborator

Egorand commented Jul 16, 2019

@afollestad btw we've introduced a large number of constants for frequently used ClassNames:

fun KClass<*>.asMutableTypeName(): ClassName {
  return when (this) {
    List::class -> MUTABLE_LIST
    Map::class -> MUTABLE_MAP
    Set::class -> MUTABLE_SET
    else -> asTypeName()
  }
}

@Egorand Egorand modified the milestones: Backlog, Icebox Aug 3, 2019
@ZacSweers
Copy link
Collaborator

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>

@ZacSweers
Copy link
Collaborator

This is filed now https://youtrack.jetbrains.com/issue/KT-35877

@seelikes
Copy link

facing the same problem, no way to create a property with MutableList type, please help

@Dutch-0
Copy link

Dutch-0 commented Jan 7, 2023

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants