Skip to content

Commit 62478fe

Browse files
committed
fix
1 parent c8664ac commit 62478fe

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

core-kotlin-modules/core-kotlin-10/src/test/kotlin/com/baeldung/variableshadowing/VariableShadowingUnitTest.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@file:Suppress("UNUSED_PARAMETER", "UNUSED_VARIABLE", "UNUSED_ANONYMOUS_PARAMETER")
1+
@file:Suppress("UNUSED_VARIABLE", "UNUSED_ANONYMOUS_PARAMETER")
22

33
package com.baeldung.variableshadowing
44

@@ -34,14 +34,14 @@ class VariableShadowingUnitTest{
3434

3535

3636
fun calculateTotalPrice(discount: Int) {
37-
val discount = 10 // Shadowing the parameter 'discount'
38-
assertEquals(10, discount)
37+
val discount = discount + 10 // Shadowing the parameter 'discount'
38+
assertEquals(30, discount)
3939

4040
val price = 100 // local variable
4141
val discountRate = 0.1
4242

4343
fun applyDiscount(price: Int): Double { // Nested function with parameter named 'price'
44-
val discountRate = 0.2 // Inner variable shadows the outer variable
44+
val discountRate = 0.2 // shadowing the outer variable discountRate
4545
return price * (1 - discountRate) // 'price' here refers to the parameter, not the outer variable
4646
}
4747

@@ -50,7 +50,6 @@ class VariableShadowingUnitTest{
5050

5151
calculateTotalPrice(20)
5252

53-
5453
val numbers = listOf(1, 2, 3, 4, 5)
5554

5655
// shadowing in loop
@@ -65,8 +64,8 @@ class VariableShadowingUnitTest{
6564
// shadowing in extension
6665
assertEquals(15, numbers.sum())
6766

68-
fun List<Int>.sum(): Int { // Shadowing occur in here
69-
var sum = 0 // shadowing built-in function sum()
67+
fun List<Int>.sum(): Int { // shadowing built-in function sum()
68+
var sum = 0
7069
this.forEach { sum += it * 2 }
7170
return sum
7271
}
@@ -76,9 +75,9 @@ class VariableShadowingUnitTest{
7675
// shadowing in lambda
7776
var sum = 0
7877

79-
numbers.forEach { value ->
80-
val value = 0 // shadowing value
81-
sum += value
78+
numbers.forEach { number ->
79+
val number = 0 // shadowing value
80+
sum += number
8281
}
8382

8483
assertEquals(0, sum)

0 commit comments

Comments
 (0)