When I tried Java’s StringBuilder
in Kotlin, I expected the following code to work:
fun main(args: Array<String>) {
val sb = StringBuilder()
sb += "hello"
sb += 1
sb += 'bit'
println(sb)
}
As of Kotlin 1.2.20, the standard library does not predefine any operator function for StringBuilder.
To make the above code work, only a single extension method is needed:
operator fun StringBuilder.plusAssign(obj: Any?) {
append(obj)
}
Is there a reason that this method is missing from the standard library, other than “too much other stuff to do”?