vertx-lang-kotlin
vertx-lang-kotlin copied to clipboard
[Codegen] Compilation error when DataObject includes MutableList
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
Based on my initial research, it does not appear possible to differentiate List and MutableList. :disappointed:
...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.