1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-08-20 05:21:26 +02:00

Revert "[scala/en] Make return value example actually demonstrate issue" (#3213)

This commit is contained in:
ven
2018-09-08 23:29:35 +02:00
committed by GitHub
parent 49a2bf306f
commit 6d087ae0f2

View File

@@ -253,20 +253,16 @@ weirdSum(2, 4) // => 16
// def that surrounds it. // def that surrounds it.
// WARNING: Using return in Scala is error-prone and should be avoided. // WARNING: Using return in Scala is error-prone and should be avoided.
// It has no effect on anonymous functions. For example: // It has no effect on anonymous functions. For example:
def addTenButMaybeTwelve(x: Int): Int = { def foo(x: Int): Int = {
val anonMaybeAddTwo: Int => Int = { z => val anonFunc: Int => Int = { z =>
if (z > 5) if (z > 5)
return z // This line makes z the return value of addTenButMaybeTwelve! return z // This line makes z the return value of foo!
else else
z + 2 // This line is the return value of anonMaybeAddTwo z + 2 // This line is the return value of anonFunc
} }
anonMaybeAddTwo(x) + 10 // This line is the return value of addTenButMaybeTwelve anonFunc(x) // This line is the return value of foo
} }
addTenButMaybeTwelve(2) // Returns 14 as expected: 2 <= 5, adds 12
addTenButMaybeTwelve(7) // Returns 7: 7 > 5, return value set to z, so
// last line doesn't get called and 10 is not added
///////////////////////////////////////////////// /////////////////////////////////////////////////
// 3. Flow Control // 3. Flow Control