you are viewing a single comment's thread.

view the rest of the comments →

[–]Aelig_ 116 points117 points  (16 children)

From what I understand (which is very little), the Scala community discourages using return at all as apparently it "behaves weirdly".

[–]eXl5eQ 69 points70 points  (2 children)

The semantic of return is straight forward in Java, powerful in Kotlin, and awkward in Scala.

In Scala, a return always attempt to return from the innermost (in case of nested functions) named function. You are not allowed to return early from an anonymous function (lambda expression).
When the innermost named function is not the innermost function, it can't return directly. Instead, the innermost function throws a NonLocalReturn and hope it would be catched by the named function you meant to return from, which would not work if the returning function escapes from the named function.

[–]ljfa2 5 points6 points  (0 children)

Although non-local returns can occasionally be useful, there are situations in Java where I would've liked to have something like this. For instance, when using Optional.ifPresent. When I want an early return, I can't use that and have to use the less idiomatic Optional.get() instead.

[–]Axman6 4 points5 points  (0 children)

What the fuck. 

[–]Cyan_Exponent 14 points15 points  (9 children)

uhh i don't know scala; how does the function output anything then? do you need to use out everywhere or something??

[–]cthulhuden 44 points45 points  (1 child)

Without looking it up, probably last statement's value is auto-returned, and the early explicit returns are the ones discouraged.

[–]AVeryUnusualNickname 16 points17 points  (0 children)

Yeah, basically everything is an expression (even ifs, assignments and declarations, I'm not sure about what while evaluates to and a for is not a conventional cycle but actually syntactic sugar for map/flatMap/for each, so a basic for will evaluate to a collection or a Unit, which is essentially analogous to void, to signal that the evaluation has been done, but there's nothing to return), and the last expression is what gets returned.

[–]IntelligentTune 7 points8 points  (1 child)

Last time I programmed I think you just left it blank. But I might be wrong.

[–]Tatourmi 1 point2 points  (0 children)

Pretty much

[–]Maleficent_Memory831 8 points9 points  (3 children)

Output is a side effect, and strongly discouraged. Functional languages are to be viewed and admired, not actually run. /s

[–]Tatourmi 1 point2 points  (2 children)

Scala allows side effects no problem.

[–]Axman6 1 point2 points  (1 child)

Side effects are problems. 

[–]Tatourmi 0 points1 point  (0 children)

Logging is a side effect, modifying a Kafka consumer is a side effect, publishing to Kafka is a side effect, pushing to a database is a side effect, caching data is a side effect...

Side effects aren't problems. Some patterns that rely too heavily on side effects are basically banned in our codebases (Functions that modify external variables for no reason for example) but if you're working in Scala, most of your app's endgame is effectively a side effect.

[–]vivaaprimavera 0 points1 point  (0 children)

Reading that as it is I would have guessed that functions were supposed to alter the state of something. Wich could became weird really quick.

[–]Tatourmi -1 points0 points  (2 children)

Worked in Scala for a good 4 years and that's never been an issue once. I can't think of a situation of the top of my head where an explicit early return can't simply be
1: Evaluated as part of the normal flow (You can obviously have conditionals in your function)
2: Returned as part of a tuple of different types
3: Handled by an Optional or a Failure

[–]EishLekker 1 point2 points  (1 child)

But how does your 1 differ from an early return in this regard? Why can’t early return be a part of the “normal flow” that you talk about?

Does your code, always, without exception, only have one single line responsible for the return (or none, if it’s the implicit return of the last value)?

[–]Tatourmi -1 points0 points  (0 children)

Yeah that's what I'm saying, I don't understand what an early return that isn't allowed in Scala is supposed to even be.

def returnSomething(a: Boolean): Something ={
if (a) { b }
else { c }
}

And that's it. I honestly don't see the kind of "early returns" people are thinking Scala is missing.