Arun
03/01/2018, 2:36 PMbuildString()
?
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/build-string.htmldiesieben07
03/01/2018, 2:38 PMbuildString {
append("Hello ")
append(name)
append('!')
}
Bit of a contrived example, since here string templates would be a cleaner solution. But it get's the idea across I think.Arun
03/01/2018, 2:44 PMdeviant
03/01/2018, 2:53 PMbuildString {
+"Hello "
+name
+"!"
}
diesieben07
03/01/2018, 3:06 PMStringBuilder
to add the necessary String
extension function. If you are willing to pay that cost you can do something like this:
inline fun myBuildString(builder: StringBuilderContext.() -> Unit): String {
return StringBuilderContext(StringBuilder()).apply(builder).toString()
}
class StringBuilderContext(private val builder: StringBuilder) {
operator fun String.unaryPlus() {
builder.append(this)
}
override fun toString() = builder.toString()
}
But I don't think it should be the default.Ruckus
03/01/2018, 3:09 PMRuckus
03/01/2018, 3:10 PM