Completed Coursera: Kotlin for developers course
This commit is contained in:
33
src/With.kt
Normal file
33
src/With.kt
Normal file
@ -0,0 +1,33 @@
|
||||
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()
|
||||
}
|
Reference in New Issue
Block a user