On 11/27/26 00:39, Paul King wrote:
Hi Jochen (apologies for the mostly AI content below),

ok, that means text wall incoming ;)

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.

but that does not mean all cases of dual identity are removed. When I get that right then

List<Function<String, String>> functions = []
functions.add { it.trim() }

will still add a closure that is also a function. My argument is very similar for this case. I think there is only a very narrow line of cases where a automatic dual identity of closure and functional interface really works.

With static types, there's no flow type to carry.

That is a definition for these GEPs, not how Groovy normally works. Just hiding the implementation via static type system does not solve the problem once you cross over to where that static type might be lost. That is reflective code and also dynamic code, but in case of a dual identity closure a instanceof is already enough to go the wrong path.

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

then make it return object and let other code inspect that. I really gave only a few examples I came up with after not even 5 minutes of thinking about the issue.

[...]
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.
so you say {it.toUpperCase()} with it in a String context under static compilation produces a call(String) method that is called from call(Object)? That would mean that

@CompileStatic
StringTransform t = { it.toUpperCase() }

Does the same, even with a proxy?

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.

I am not talking about lambdas at the moment, only about closures. Lambdas using groovy.lang.Closure was always a helper construct, so I consider the rules being different for those compared to Closure.

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.

The difference is the dual identity. Yes, {String it -> it.toUpperCase()} fails in dynamic Groovy for call(1). It fails basically inside the Closure. But in the proxy variant there is no groovy.lang.Closure to cast to. If I extend to reflective Java code, then you see that in the dual entity case I can have a Closure, while in the proxy case I have only the Interface. Any call I do from there will use the interface then. Both my fail from dynamic Groovy with a MissingMethodException, but for different reasons and with different effects. I fail to consider them equal.

bye Jochen

Reply via email to