1
+ @file:Suppress(" UNUSED_PARAMETER" , " UNUSED_VARIABLE" , " UNUSED_ANONYMOUS_PARAMETER" )
2
+
1
3
package com.baeldung.variableshadowing
2
4
3
5
import org.junit.jupiter.api.Assertions.assertEquals
@@ -70,7 +72,7 @@ class VariableShadowingUnitTest{
70
72
71
73
// in lambda
72
74
var sum = 0
73
- var values = listOf (1 , 2 , 3 , 4 , 5 )
75
+ val values = listOf (1 , 2 , 3 , 4 , 5 )
74
76
75
77
values.forEach { value ->
76
78
val value = 0
@@ -80,95 +82,4 @@ class VariableShadowingUnitTest{
80
82
assertEquals(0 , sum)
81
83
}
82
84
83
- // @Test
84
- // fun `local variable or nested function shadowing`() {
85
- //
86
- // fun calculateTotalPrice() {
87
- // val price = 100 // local variable
88
- // val discountRate = 0.1
89
- //
90
- // fun applyDiscount(price: Int): Double { // Nested function with parameter named 'price'
91
- // val discountRate = 0.2 // Inner variable shadows the outer variable
92
- // return price * (1 - discountRate) // 'price' here refers to the parameter, not the outer variable
93
- // }
94
- //
95
- // assertEquals(80.0, applyDiscount(price))
96
- // }
97
- //
98
- // calculateTotalPrice()
99
- // }
100
-
101
- // @Test
102
- // fun `loop variable shadowing`() {
103
- // val numbers = listOf(1, 2, 3, 4, 5)
104
- // for (number in numbers) {
105
- // val doubledNumber = number * 2
106
- // val number = number * 2 // Shadowing the loop variable 'number'
107
- //
108
- // assertEquals(doubledNumber, number)
109
- // }
110
- // }
111
-
112
-
113
- @Test
114
- fun `parameter shadowing` () {
115
- fun calculateTotalPrice (discount : Int ) {
116
- assertEquals(300 , discount)
117
-
118
- val discount = 10 // Shadowing the parameter 'discount'
119
- assertEquals(10 , discount)
120
-
121
- val price = 100
122
- val totalPrice = price - discount
123
- assertEquals(90 , totalPrice)
124
- }
125
-
126
- calculateTotalPrice(300 )
127
- }
128
-
129
- // @Test
130
- // fun `nested function variable shadowing`(){
131
- // fun calculateDiscount(originalPrice: Double) {
132
- // val discountRate = 0.1 // Outer variable
133
- //
134
- // fun applyDiscount(price: Double): Double { // Nested function with parameter 'price'
135
- // val discountRate = 0.2 // Inner variable shadows the outer variable
136
- // return price * (1 - discountRate)
137
- // }
138
- //
139
- // val discountedPrice = applyDiscount(originalPrice)
140
- // println("Discounted price: $$discountedPrice")
141
- // }
142
- //
143
- // calculateDiscount(200.0)
144
- // }
145
-
146
- // @Test
147
- // fun `variable shadowing in lambda`(){
148
- // var sum = 0
149
- // var values = listOf(1, 2, 3, 4, 5)
150
- //
151
- // values.forEach { value ->
152
- // val value = 0
153
- // sum += value
154
- // }
155
- //
156
- // assertEquals(0, sum)
157
- // }
158
-
159
- // @Test
160
- // fun `shadowing in extension`(){
161
- // val numbers = listOf(1, 2, 3, 4, 5)
162
- //
163
- // assertEquals(15, numbers.sum())
164
- //
165
- // fun List<Int>.sum(): Int { // Shadowing occur in here
166
- // var sum = 0 // shadowing built-in function sum()
167
- // this.forEach { sum += it * 2 }
168
- // return sum
169
- // }
170
- //
171
- // assertEquals(30, numbers.sum())
172
- // }
173
-
174
85
}
0 commit comments