I like kotlin's += syntax for lists.
Its a concise and intuitive notation.
But recently I discovered an edge case, where I suspect, the compiler is buggy.
java.nio.file.Path is defined recursively public interface Path extends Iterable<Path>, ...
And that seems to cause problems for Iterable's plus operator in kotlin-stdlib.
private val abc = Path.of("/tmp/abc.txt")
private val def = Path.of("/tmp/def.txt")
@Test
fun `plus operator List Path`() {
var list = emptyList<Path>()
list += listOf(abc)
list += def // (*) should not compile
list.asClue {
// oops. red bar.
it shouldBe listOf(abc, def)
}
}
The test fails with:
Elements differ at index 1: [\tmp\abc.txt, tmp, def.txt]
In my opinion the line marked with (*) shouldn't even compile, since it is not clear which of the following overloads is intended
Notice that for plusAssign operator on MutableList compilation is rightly rejected.
@Test
fun `plusAssign operator MutableList Path`() {
val list = mutableListOf<Path>()
list += listOf(abc)
// does not compile, which is good
// list += def // (**)
list shouldBe listOf(abc)
}
The line marked with (**) is ambiguous for