diff options
author | Adam Bard <github@adambard.com> | 2018-09-08 09:50:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-08 09:50:37 -0700 |
commit | 49a2bf306fcf365192934d006a244a9033909850 (patch) | |
tree | d12a60506c59e9396447b32088240e1d8f954199 /scala.html.markdown | |
parent | e6b01188bf23d747c190b201fa7f0dafec31782c (diff) | |
parent | 23cee36b4c056dcd3c01676132cb9a6588fb75ad (diff) |
Merge pull request #2769 from cincodenada/patch-2
[scala/en] Make return value example actually demonstrate issue
Diffstat (limited to 'scala.html.markdown')
-rw-r--r-- | scala.html.markdown | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/scala.html.markdown b/scala.html.markdown index 28424684..7429ac9a 100644 --- a/scala.html.markdown +++ b/scala.html.markdown @@ -253,16 +253,20 @@ weirdSum(2, 4) // => 16 // def that surrounds it. // WARNING: Using return in Scala is error-prone and should be avoided. // It has no effect on anonymous functions. For example: -def foo(x: Int): Int = { - val anonFunc: Int => Int = { z => +def addTenButMaybeTwelve(x: Int): Int = { + val anonMaybeAddTwo: Int => Int = { z => if (z > 5) - return z // This line makes z the return value of foo! + return z // This line makes z the return value of addTenButMaybeTwelve! else - z + 2 // This line is the return value of anonFunc + z + 2 // This line is the return value of anonMaybeAddTwo } - anonFunc(x) // This line is the return value of foo + anonMaybeAddTwo(x) + 10 // This line is the return value of addTenButMaybeTwelve } +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 |