34 lines
717 B
Kotlin
34 lines
717 B
Kotlin
![]() |
fun main() {
|
||
|
val sb = StringBuilder()
|
||
|
// sb becomes this within the lambda scope
|
||
|
val string = with(sb) /* Is an extention function on type T */ {
|
||
|
appendLine("Something")
|
||
|
append('a')
|
||
|
toString()
|
||
|
}
|
||
|
|
||
|
val buildString = buildString {
|
||
|
append(1)
|
||
|
append(2)
|
||
|
toString()
|
||
|
}
|
||
|
|
||
|
val words = Words()
|
||
|
with(words) {
|
||
|
// The following two lines should compile:
|
||
|
"one".record()
|
||
|
+"two"
|
||
|
}
|
||
|
words.toString() == "[one, two]"
|
||
|
}
|
||
|
|
||
|
class Words {
|
||
|
private val list = mutableListOf<String>()
|
||
|
|
||
|
fun String.record() = list.add(this)
|
||
|
|
||
|
operator fun String.unaryPlus() = list.add(this)
|
||
|
|
||
|
override fun toString() = list.toString()
|
||
|
}
|