-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScopedFunctions.kt
48 lines (42 loc) · 1.13 KB
/
ScopedFunctions.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class A {
var x: Int = 0
var y: Int = 0
override fun toString(): String {
return "A<x:$x, y:$y>"
}
}
fun main() {
val a = A().apply {
// Object reference using this, returns object on which it is called
x = 4
y = 8
}
println(a)
val b = A().run {
// Object reference using this, returns result of last statement (in case no result means kotlin.Unit)
x = 4
y = 8
}
println(b)
val c = A().let {
// Object reference using it (or name), returns result of last statement (in case no result means kotlin.Unit)
it.x = 4
it.y = 8
it.x == it.y
}
println(c)
val d = A().also {
// Object reference using it (or name), returns object on which it is called
it.x = 4
it.y = 8
it.x == it.y
}
println(d)
val e = with(A()) {
// Object reference using this, returns result of last statement (in case no result means kotlin.Unit)
// Differs with run in one way that this is not an extension function while run is
x = 4
y = 8
}
println(e)
}