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

[Codegen] Compilation error when DataObject includes MutableList #161

Open
rgmz opened this issue Dec 9, 2019 · 1 comment
Open

[Codegen] Compilation error when DataObject includes MutableList #161

rgmz opened this issue Dec 9, 2019 · 1 comment

Comments

@rgmz
Copy link
Contributor

rgmz commented Dec 9, 2019

Given the following (contrived) data object:

@DataObject(generateConverter = true)
class StudentList() {
    var names: MutableList<String> = mutableListOf()

    constructor(json: JsonObject) : this() {
        StudentListConverter.fromJson(json, this)
    }

    fun toJson(): JsonObject = JsonObject.mapFrom(this)
}

The generated helper functions will incorrectly use toList() (IMMUTABLE) instead toMutableList():

fun studentListOf(names: Iterable<String>? = null): StudentList = io.vertx.example.StudentList().apply {
  if (names != null) {
    this.names = names.toList()
  }
}

Which results in a compilation error (inferred type is List<String> but MutableList<String> was expected):

> Task :compileKotlin FAILED

e: ~/foo/build/generated/source/kaptKotlin/main/io/vertx/example/StudentList.kt: (34, 22): Type inference failed. Expected type mismatch: inferred type is List<String> but MutableList<String> was expected
@rgmz
Copy link
Contributor Author

rgmz commented Dec 10, 2019

Based on my initial research, it does not appear possible to differentiate List and MutableList. 😞

...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.

Edit: you can seemingly cast toList() as toList() as MutableList<String>, although I'm not certain the impact of this.

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

No branches or pull requests

1 participant