#scala
// Inline add and addR
def sum(ns: Int*): Int = ns.foldLeft(0)((n, m) => n + m) // inlined add
scala> sum(33, 42, 99)
res2: Int = 174 // alright
def sumR(ns: Int*): Int = ns.foldLeft(0)((n, m) => return n + m) // inlined addR
scala> sumR(33, 42, 99)
res3: Int = 33 // um.
If you want to change selection, open document below and click on "Move attachment"
tpolecat(addR)
scala> sumR(33, 42, 99)
res1: Int = 174
So far so good. There is no apparent difference between sum and sumR which may lead you to think that return is simply an optional keyword. But let's refactor a bit by inlining add and addR.
<span>// Inline add and addR
def sum(ns: Int*): Int = ns.foldLeft(0)((n, m) => n + m) // inlined add
scala> sum(33, 42, 99)
res2: Int = 174 // alright
def sumR(ns: Int*): Int = ns.foldLeft(0)((n, m) => return n + m) // inlined addR
scala> sumR(33, 42, 99)
res3: Int = 33 // um.
What the what?
So, the short version is:
A return expression, when evaluated, abandons the current computation and returns to the caller of the method in which return appears.
So in Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details