Skip to content

Commit fe4d7a4

Browse files
committed
minor tweeks to show if() is an expression
1 parent ab86574 commit fe4d7a4

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/main/kotlin/me/kotlindemo/01_IntroToKotlin.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,17 @@ fun someVariableStuff() {
142142
//IF as an expression so no ternary in kotlin (not same as elvis) ie if statements are expressions and return a value
143143
fun ifAsAnExpressionMaxValue(a: Int, b: Int) = if (a > b) a else b
144144

145-
fun ifExpressionReturnsAValueWithBlocks(a: Int, b: Int): Int =
146-
if (a > b) { //if this was a block body, return requred here on the if since if is an expression. return is required for block functions vs expression body
145+
fun ifExpressionReturnsAValueWithBlocks(a: Int, b: Int): Int { //block body
146+
return if (a > b) { //if is an expression thus returns the value
147147
println(" if: a is bigger $a > $b")
148148
a //return a, 'return' not required since this is an expression body syntax, ie no {} for the block
149149
} else {
150150
println(" if: b is bigger $b > $a")
151151
b //return b
152152
}
153+
}
153154

154-
//when replaces switch. when can be used as an expression or a statement, excuted sequentially returns a string
155+
//when replaces switch. when can be used as an expression or a statement, executed sequentially returns a string
155156
fun simpleWhenAsExpression(a: Int) =
156157
when(a) {
157158
in 0..5 -> " range: a in 0..5 range: $a"
@@ -164,7 +165,7 @@ fun simpleWhenAsExpression(a: Int) =
164165
*/
165166
fun whenWithSmartCast(a: Any) =
166167
when(a) {
167-
is String -> println(" smartcast: got a string: $a") //in block we don't have to do typcast, its automagic
168+
is String -> println(" smartcast: got a string: $a") //in block we don't have to do typecast, its automagic
168169
else -> println(" smartcast: not a string $a")
169170
}
170171

0 commit comments

Comments
 (0)