Hi OG,

oh my, you opened a can of worms there - I know what I am talking about, since I opened it already a few years back ;-)

Replying to the easy part first:

1. Support for "in" is, if memory serves, a relative new addition to
   Groovy.
    1. I agree that it is quite intuitive in Groovy, though I never use
       for loops, so my code only has a few if(foo in [ ... ]) code pieces.
    2. However for Java I agree with the colon choice, based on the
       concept that introducing new keywords in a minimalistic,
       no-magic language like Java if not strictly necessary is not a
       good idea, and leads to a Basic-/Ruby-ization of it.
        1. (I started my programming life using several different Basic
           dialects (C64, GFA, Omikron), and was forced to use several
           in my IT caree (Access, VB.NET, Lotus Script), and I can
           therefore say with confidence that it is, and never was, a
           well designed/elegant language by any measure; so much for
           "we always just love what we first encountered/used in our
           youth" ;-) )
2. I personally love stream-like APIs in any language, it is exactly
   how my mind thinks about things.
    1. foo in foos or foo : foos to me is less intuitive than to say
       foos.each, since that puts the Iterable at center stage, and the
       "loop" variable (if it does not suffice) comes later.
3. You do not get to choose what idiomatic Groovy is, since
   iterable.each has been around forever, it's only competition at the
   time being using the foos iterator explicitly in a C-like while loop ;-)
4. But that is not what you wanted to talk about - so on to email #2...


Cheers,
mg


On 04/12/2024 18:03, OCsite wrote:
MG,

On 4. 12. 2024, at 16:11, MG <mg...@arscreat.com> wrote:

     1. e.g. using a for(foo : foos) { ... } loop instead of
        canonical Groovy foos.each { foo -> ... }, to be able to
        easily return from the for body from multiple places using
        return statements.

For one, I would argue that the native and groovier (since more logical and intuitive and intention-revealing for anyone who can read English completely regardless whether he knows Java or not) variant should be the /for/in/ loop, like /for (foo *in* foos)/. That weird and unintuitive colon thing should, in my personal opinion, remain limited to code copy/pasted from Java (exactly like /var/ :))

That would not be worth an extra email though. I wonder, would it be perhaps worth the effort to extend the language by adding a support for method-returning from a closure?

A trivial (and most probably very very wrong and problems-inducing!) approach might perhaps be an ASTT which would convert code like

def foo() {
  bar.each { if (it) methodreturn it }
}

to something like

def foo() {
  try {
    bar.each { if (it) throw new MethodReturnException(value:it) }
  } catch (MethodReturnException e) {
    return e.value
  }
}

Would it be perhaps worth the effort to add such an ASTT to Groovy? Not sure at all... but it might help (a) to stick with the canonical /foo.each/ instead of enforcing /for/ins/, (b) also, in many cases like /foo.find/, /foo.allResults/, et cetera, which are even more ugly to replace with plain ole loops.

All the best,
OC

Reply via email to