> > I'm not sure why you thought I was suggesting you were
> > trying to say Scala isn't an improvement over Java. I
> > didn't get that impression. The "irony" I saw was that
> to
> > get at the benefits of the functional style in Java,
> Java
> > syntax actually pushed you in the other direction.
>
> I guess I get that impression because you keep telling
> about features of Scala when I already know Scala, or at
> least all the features you are talking about. Since I
> have tried to make that clear a couple times, it makes me
> think that you are not understanding me. I'm angry or
> anything, I'm just want to communicate effectively.
>
Sorry I wasn't sure how much of Scala you've seen, but also I'm telling others who are reading the discussion who aren't familiar with Scala. But mainly my irony comment was more about how I found it interesting that although you seem to have naturally stumbled upon some of the values that I only discovered when I went to Scala (like preferring little helper methods, avoiding vars, etc.), when trying to write that way in Java, it pushed you in a direction of multiple returns, the opposite direction it pushed me in Scala. The difference is in the language influence, I think. Because Java has mostly imperative control structures that don't result in a value, it makes it harder to
do the seeing methods as single expressions approach.
> > As far as using the boolean variable, that's not me
> being
> > unfair to Java. That's me being true to how I
> programmed
> > in Java for 10 years, and how I think most people
> program
> > in Java from the code I've seen over the years. That's
> the
> > idiom, and returning from inside a for loop, which you
> > showed, would I think be frowned upon by many Java
> > programmers.
>
> And again, this is exactly my point. It's not a feature
> of Java. It's something that people do in Java because
> think Java is like C. Given that Java is not really much
> like C, I fail to understand why it makes any more sense
> to use this idiom in Java than it does to use it in
> Scala.
>
Yes, some of it may be tradition, but how can you tell the difference between when people's preference is because they are used to that way, they were taught that way, etc., versus that they *really* feel that way.
> Are you saying that we must accept this as part of Java
> merely because some people say we should? If developers
> starting using this idiom in Scala does it become part of
> Scala?
>
> > Which rules to you mean? Sorry, can you be more
> explicit?
> > If you mean he rule of having just one return, that
> > doesn't quit add up because in Scala that's also the
> rule
> > of thumb, except that you see it as one expression that
> > doesn't need an explicit return.
>
> The source for List.exists() follows:
>
> override def exists(p: A => Boolean): Boolean = {
> var these = this
> while (!these.isEmpty) {
> if (p(these.head)) return true
> these = these.tail
> }
> false
> }
>
> Caveat: it's possible that the author of this method is
> not familiar with the idioms of Scala or of the single
> return rule of thumb in general.
>
Well, it was probably Martin who wrote that. That's a great example that ties everything together. Pretty impressive. Did you plan that or was it just a coincidence?
Anyway, I would say this is idiomatic Scala in the sense that one of the good reasons for using the imperative style is for performance. There are other good reasons. I think "Scala style" is lean towards functional by default, use imperative when you have a good reason and justification.
List needs to be fast, though I not sure why the while loop being used would be faster than a tail recursive loop that uses recursion, which would have gotten optimized into a a while loop in the binaries. I'm pretty sure it would be tail recursive. Anyway, once they picked the while approach, they would have returned out of the middle because there is no break in Scala. Either you return out of the middle, or you set the flag and keep looping to the end, or check the flat each time through the while loop. The fastest of these would be the return from the middle.