Skip to content

Commit e25f748

Browse files
authored
Merge pull request #852 from hangga/implicit-qualified-this-simple
KTLN-394 - Qualified this and implicit this in Kotlin
2 parents 924e749 + e225260 commit e25f748

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.baeldung.implicitAndQualifiedthis
2+
3+
4+
import org.junit.jupiter.api.Assertions.assertEquals
5+
import org.junit.jupiter.api.Test
6+
7+
class ImplicitAndQualifiedthisUnitTest {
8+
9+
@Test
10+
fun `Qualified this and implicit this Test`() {
11+
12+
13+
fun printLine() = "Top-level function"
14+
val x = 5
15+
16+
class Outer {
17+
fun foo() : Int = 30
18+
val x = 0
19+
val a = this
20+
21+
fun printLine() = "Member function"
22+
23+
fun checkThis() {
24+
val b = this // Implicit: refers to the current instance of Outer
25+
assertEquals(Outer::class.java.name, b::class.java.name)
26+
27+
assertEquals(0, this.x) // Implicit: refers to the x property of the current Outer instance
28+
assertEquals(5, x) // Implicit: refers to the x property of the current Outer instance
29+
30+
assertEquals("Member function", this.printLine())
31+
assertEquals("Top-level function", printLine())
32+
33+
fun checkThisToo(){
34+
val c = this
35+
}
36+
}
37+
38+
inner class Inner {
39+
40+
val x = 1
41+
42+
fun checkInner(){
43+
44+
val funLit = fun String.() {
45+
assertEquals("test.funLit()", this) // Implicit: receiver of the function literal (the String itself)
46+
}
47+
48+
"test.funLit()".funLit()
49+
50+
val funInnerLambda = { _: String ->
51+
val r = this
52+
assertEquals(Inner::class.java.name, r::class.java.name)
53+
}
54+
55+
funInnerLambda("test inner lambda")
56+
57+
val numbers = listOf(0, 1, 2, 3, 4)
58+
numbers.forEach { number ->
59+
number.run {
60+
assertEquals(number, this)
61+
}
62+
}
63+
64+
val someNumber = 10
65+
66+
someNumber.run {
67+
val numberLambda = { _: String ->
68+
assertEquals(someNumber, this)
69+
}
70+
numberLambda("numberLambda")
71+
}
72+
73+
val someObject = object {
74+
val name = "John"
75+
val age = 30
76+
}
77+
78+
someObject.run {
79+
assertEquals(someObject, this)
80+
81+
val someLambda = { _: String ->
82+
assertEquals(someObject, this)
83+
assertEquals(30, this.age)
84+
assertEquals("John", this.name)
85+
}
86+
87+
someLambda("someLambda")
88+
}
89+
}
90+
91+
fun Int.foo() {
92+
val x = 2
93+
val y = this
94+
assertEquals("Int", y::class.simpleName)
95+
96+
assertEquals(2, x)
97+
98+
assertEquals(42, this) // Implicit: receiver of the extension function (the Int value 42)
99+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
100+
101+
assertEquals(this, this@foo) // Both this keywords refer to the same receiver (42)
102+
103+
fun Int.bar() {
104+
assertEquals(32, this) // Implicit: receiver of the extension function (the Int value 32)
105+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
106+
107+
fun Int.baz(){
108+
assertEquals(20, this) // Implicit: receiver of the extension function (the Int value 20)
109+
assertEquals(42, this@foo) // Qualified: specifies the receiver of the extension function (42)
110+
}
111+
20.baz()
112+
}
113+
32.bar()
114+
115+
assertEquals(Inner::class.java.name, this@Inner::class.java.name) // Qualified: Inner class
116+
assertEquals(Outer::class.java.name, this@Outer::class.java.name) // Qualified: Outer class
117+
118+
assertEquals(1, this@Inner.x) // Qualified: specifies x of the Inner class
119+
assertEquals(0, this@Outer.x) // Qualified: Outer's x property
120+
assertEquals(30, this@Outer.foo()) // Qualified: Outer's foo() function
121+
122+
val funLit = fun String.() {
123+
assertEquals("test.funLit()", this) // Implicit: receiver of the function literal (the String itself)
124+
}
125+
126+
"test.funLit()".funLit()
127+
128+
val funExtLambda = { _: String ->
129+
val r = this
130+
assertEquals("Int", r::class.simpleName) // Implicit: this inside a lambda with no receiver refers to the enclosing context (Int.foo())
131+
}
132+
133+
funExtLambda("test funExtLambda")
134+
135+
val increase = fun(x: Int): Int {
136+
return this + x
137+
}
138+
assertEquals(43, increase(1))
139+
}
140+
}
141+
}
142+
143+
val outer = Outer()
144+
outer.checkThis()
145+
outer.foo()
146+
147+
val inner = outer.Inner()
148+
inner.checkInner()
149+
inner.run {
150+
42.foo() // Calls the extension function defined inside Inner
151+
}
152+
153+
}
154+
}
155+
156+

0 commit comments

Comments
 (0)