Hi Jochen (apologies for the mostly AI content below),

Thanks, that's a good question to pin down. I ran each case on 5.1.3,
6.0.0 and on a build with GEP-33/34/35, and I've updated GEP-33 and
GEP-35 to state the answer explicitly.

For your example, nothing changes:

    StringTransform myTransform = { it.toUpperCase() }

has a declared target, so it stays a coercion proxy, as in 6.0.0.
GEP-34 v2 deliberately leaves closures at declared targets alone. The
value is a StringTransform and never a Closure.

With static types, there's no flow type to carry. The static type is
the interface, so under @CompileStatic or @TypeChecked:

    myTransform.call(1)   // compile error: checked against transform(String)
    Closure<String> m() { StringTransform t = { it.toUpperCase() }; return t }
                          // compile error: a StringTransform is not a Closure

That's already true in 5.1.3 and 6.0.0. The information can't be lost
by leaving the context, because the value doesn't acquire an
unconstrained call(Object) view unless you cast it to Closure.

In dynamic code there are no types to lose anyway. GEP-35 makes
f.call(x) mean exactly the interface method, with the same argument
matching as calling that method directly. So call(1) fails the same
way transform(1) does, with a MissingMethodException. It accepts no
argument that the interface method would reject.

There are two caveats, and both exist today:

1. Erasure. For a generic interface like Function<String, String>, the
run-time method is apply(Object). So dynamic f.call(1) on a native
lambda reaches the lambda and fails there with a ClassCastException.
f.apply(1) already does exactly that in 6.0.0, and so does a raw call
in Java. Only static types can check the type arguments, and they do.

2. A Closure view after a cast. Under GEP-33, a closure stored in a
type-parameter slot under @CompileStatic, as in l.add { ... } on a
List<StringTransform>, is a closure that also implements the
interface. Through the interface it's checked like a proxy, and
statically l[0] is a StringTransform, so returning it as a
Closure<String> is still a compile error. Only after an explicit
(Closure<String>) cast does call(Object...) take anything, and a wrong
argument then fails in the body. That's the same as every Groovy
closure today: Closure<V> types the result, never the parameters. I've
added an open question to GEP-33 on whether that view should be
stricter. My current view is to keep it, because it matches what a
native lambda does with an erased generic signature.

If you have other cases in mind, send them over and I'll run them too.

Cheers, Paul

On Fri, Nov 27, 2026 at 7:22 AM Jochen Theodorou via dev
<[email protected]> wrote:
>
> On 11/26/26 13:56, Paul King wrote:
> [...]
> > GEP-33: Functional Literals for Type-Parameter Slots
> > GEP-34: Native Functional Values
> > GEP-35: Call Syntax on Functional Values
> >
> > I plan to roll back just a few items for 6.0.1 related to those topics
> > where the inconsistencies and crashes occurred. PRs targeting
> > GROOVY_6_0_X reflect what I propose. We can potentially bring more of
> > them across for 6.0.2 if we find the right solution. I am trying to
> > avoid folks having to flip to a new 6 semantics and then flip to
> > something different (or flip back) for 7.[...]
>
> Hi Paul... after looking at those I see some potential problems.
>
> so assume we have
>
> interface StringTransform {
>    String transform(String)
> }
>
> class Uppercase implements StringTransform {
>    String transform(String x){x.toUppercase()}
>    int getID(){1}
> }
>
> obviously I can do
>
> StringTransform  myTransform = new Uppercase()
> myTransform.getID()
> myTransform.transform("foo")
>
> And the proposal seems to suggest that if we allow this:
>
> StringTransform myTransform = { it.toUppercase() }
> myTransform.transform("foo")
> myTransform.call("foo")
>
> but what is with:
>
> myTransform.call(1)
>
> How do we prevent this? And even if we say that we will carry a virtual
> flow type for this that keeps the information, we loose that information
> as soon as we leave the context. A return myTransform in a method that
> returns Closure<String> for example sill does not define what argument
> types are legal for call(Object).
>
> There is much more in there, but I have the feeling this a pretty
> central aspect for the goal
>
> bye Jochen
>
>
>

Reply via email to