There are several times when I ran a simple template when programming in Java or C ++, for which the user control structure could reduce the template in my code. It looks something like this:
if( Predicate ){
Action
return Value
}
that is, the "return if" statement. I tried making functions with signatures like foo[A,B]( pred:((A,A)=>Boolean), value:Option[B] ), but then I finish checking if I returned Some or None. I am disabled by the operator return.
Is there a way to inherit such control structures in functional languages or, more specifically, Scala?
Edit:
I was not so clear with my description, and this confused people who are trying to help me. The main reason mine foodoesn't work is because it cannot short-circuit the evaluation of the contained function. it
def intersect( geometry:Geometry, reference:Geometry ):Geometry = {
return_if( withinBounds( geometry, projection ), logToString( logger, "Geometry outside " + projection.toString ), EmptyGeometry() )
return_if( topologicallyCorrect( geometry ), intersect( correct( geometry ), reference )
}
return_if.