Reputation: 1473
I'm not sure how to use the return
keyword in an anonymous function (or perhaps I should solve my problem in a different way?).
The way it is right now, the return
actually refers to the enclosing function.
()=>{
if (someMethodThatReturnsBoolean()) return true
// otherwise do stuff here
}:Boolean
Upvotes: 0
Views: 280
Reputation: 32719
The way it is right now, the return actually refers to the enclosing function.
This is how it is supposed to be. You cannot use return
for returning from an anonymous function. You'll have to rewrite your code to avoid the return
statement, as in:
()=>{
if (someMethodThatReturnsBoolean()) true
else {
// otherwise do stuff here
}
}:Boolean
Upvotes: 4
Reputation: 10411
Why not ?
() =>
someMethodThatReturnsBoolean() || {
//do stuff here that eventually returns a boolean
}
Or, if you don't like to produce side effects with the ||
operator you can just use plain if:
() =>
if (someMethodThatReturnsBoolean())
true
else {
//do something here that returns boolean eventually
}
if
is just an expression in Scala and you should organize your code in an expression-like fashion and avoid return
as much as possible.
Upvotes: 5