Saturday 15 March 2014

Return from "for" in Scala is not the same as return from "for" in Java -


I have a Java code, which I want to convert to Scala:

  MyClass MyMethod (String Straight 1) {Items (items: items) {if (/ * some condition * /) {if (/ * another condition * /) {return item.myMethod123 (); }}} Return super.myMethod (str1); }   

If I use in in Scala, then it will be translated into map , which will return to return Calling will return the price from map to only map , but it can not stop the myMethod execution .

UPDATE : I mean, I have to do this using for foreach instead of However, Returning foreach to foreach from return is not returning and execution of myMethod .

UPDATE2 : I'm confused, foreach does not return any value.

this will actually be stop myMethod execution Because behind the scenario it actually throws an exception (non-stack-trace) exception before you actually got caught by myMethod then you just

  code Str1: string) {for (item & lt; - items) {if (/ * some cond * /) {if (/ * other cond * /) {Return item.myMethod123}}} super.myMethod (str1)} < / Code>  

"just like" in java

The code works fine; It does not work as fast as it contains an exception (stack traces that take most of the time, so you probably are right here until it has a different loop.)

Alternatively, you

  val target = item. Remove {item = & gt; If (/ * some cond * /) {/ * other cond * /} and false} target.map (_ myMethod123 ()). GetOrElse (super.myMethod (str1))   

before selecting that item on which you can call a method, or if there is no such thing, super omissions Will go.

No comments:

Post a Comment