On 29.03.20 14:17, Paul King wrote:
Groovy's original rule was that a newline terminates a statement when it
could sensibly finish (successfully complete an expression) at that
point. So if you've opened a curly brace or bracket, for instance, the
newline would not auto-terminate an expression/statement.

The rule was later expanded with some lookahead to also merge lines when
the subsequent line couldn't be a valid statement in its own right. This
allows the following for example in Groovy 3:

def truth = maybe
   && possibly

For me the whole problem here is actually Java. People did always write
things similar to this:

"""
memberNames.stream().filter((s) -> s.startsWith("A"))
                     .map(String::toUpperCase)
                     .forEach(System.out::println);
"""

and IDEs like Intellij IDEA are actually forcing you into this style
quite a bit. The dot is not our problem though, it is the curly brace,
plus and minus in combination with optional return.

"""
def x = "some long text"
        + "another line"
"""

As we all know for Groovy this can mean two expression statements or one
while in

"""
def x = "some long text" +
        "another line"
"""

it is very clear for the compiler that the lines belong together. So if
we make the first PLUS - example two statements we essentially "broke"
Java code.


So I was thinking we could do something intelligent with the compiler
and react differently if we have the last line or not... only that will
not solve the Spock problem at all.

So I have another alternative to suggest (besides ditching trying to be
so exact source compatible with Java), and that is:

* make plus and minus always align to the line before, essentially
disabling standalone unary plus and minus. If you want to return them,
you will have to write return.

* make Closure and block on the next line always detached from the line
before. If they are supposed to belong together, then the curly brace
has to be on the line before.


Coming back to the examples before:

```
meth
{ p ->
}
```

and

```
a | meth
{ p ->
}
```

will be two expression statements, while

```
meth { p ->
}
```

will be one.

And yes, I fully expect this to break existing code. And yes, that is
basically an inconsistent rule, since we handle +/- different from {...
though there is a difference between a syntax element like { and a
binary/unary operator

bye Jochen





Reply via email to