Okay, I have a draft PR here:

https://github.com/apache/groovy/pull/1910

The highlights:
* extends the Groovy runner to run JEP 445 like classes (to match and
slightly exceed what is covered by the new JEP 445 launch protocol)
* supports JEP 445 instance method syntax (for cut-n-paste compatibility,
but not static examples)
* retains our currently supported static main support (but extends it to
also handle no-arg variants)
* provides support for providing your own run method (to give JEP 445 like
syntax but still creates a class that extends Script)

I think this is a reasonable compromise that offers some new useful
features while retaining close to full backwards compatibility (just one
edge case is not covered). However, my gut feel is that we might be better
to forgo some additional backwards compatibility for increased consistency.
We could offer a complete superset of JEP 445 functionality by supporting
both instance and static methods and use the new override "run" method
functionality to cover what we currently obtain with our static main
support.

This alternative would be easier to explain:
* if there is no run or main method, then it's a Script and needs @Field to
create scriptclass-level fields, otherwise declaration statements
correspond to local variable declarations
* even if it has a run or main method but the script contains "loose"
statements (i.e. anything that wouldn't correspond to a field or method
definition), then it's a Script (and you might get a duplicate method
compilation error)
* if it has no "loose" statements and a "run" method, it's a Script (but no
need for @Field and some free annotation sharing)
* if it has no "loose" statements and a "main" method, it's a JEP 445
script (also no need for @Field, has some free annotation sharing, but no
access to binding or context)

Thoughts? Should I create an alternative PR for comparative purposes?

Cheers, Paul.



<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Virus-free.www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

On Thu, Jun 29, 2023 at 3:20 PM Paul King <pa...@asert.com.au> wrote:

>
> I have started exploring what it might mean for Groovy to support/co-exist
> with the JEP-445 "scripting" capabilities that are being offered as preview
> features in JDK21. I am documenting what that might entail here:
>
> https://issues.apache.org/jira/browse/GROOVY-11118
>
> It will likely be next week before it is really ready for discussion but
> if folks are particularly interested, please feel free to send me some
> comments.
>
> At this early stage, the TL;DR version of what I think it might look like
> is as follows:
> * JEP 445 supports source files with no explicit class declaration but
> they must contain a static or instance main method with no args or a
> String[] argument
> * we already support scripts which have no class declaration including
> ones with a static main method but we don't support instance main methods
> (they conflict with the automatically added main which is part of the
> scripting model)
> * we can support instance main methods and in such a case we can offer a
> JEP445-like experience
> * Groovy's runner protocol can be extended to support the revised JEP-445
> launch protocol (essentially giving an option for launching JEP-455
> compatible scripts from JDK11 onwards)
>
> Cheers, Paul.
>
>
>
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> Virus-free.www.avast.com
> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
> <#m_-5111629535372135170_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>

Reply via email to