New candidate JEP: 445: Flexible Main Methods and Anonymous Main Classes (Preview)

Brian Goetz brian.goetz at oracle.com
Mon Apr 17 14:50:33 UTC 2023


If the blocks in your proposal are not instance initializers, this 
unfortunately makes it worse:

  - Even ignoring any possible syntactic ambiguity with instance 
initializers, now there are two identical-looking constructs in the 
language that mean subtly different things based on context.

  - The "main blocks" are even more of an on-ramp-to-nowhere, since they 
would be illegal (or worse, mean something else!) in a full class.  Even 
without the conflict, this is creating a dead-end dialect for the sake 
of avoiding a few keystrokes.

Ultimately I think this proposal is motivated more by minimizing 
character count than by reducing the number of concepts that simple 
programs must use, and as a result, ends up creating net new complexity.



On 4/17/2023 10:24 AM, Andrew Evers wrote:
> Hi,
>
> 1. Understood.
>
> 2. I'm not proposing instance initializers, but this has caught two 
> people now.
>
> I'm proposing that absent a class declaration, the first block _is_ 
> the main method. You could even argue for the removal of the braces, 
> but that makes adding methods difficult. This is similar to the shell, 
> python, PHP, and PL/SQL anonymous blocks.
>
> On Mon, Apr 17, 2023 at 10:14 AM Brian Goetz <brian.goetz at oracle.com> 
> wrote:
>
>
>     > 1. Would it be at all possible to consider a main that returns int?
>     > Operating systems that don't support it are free to disregard
>     it, and
>     > it feels better than System.exit(). It also allows skipping the
>     idea
>     > of void, which is somewhat of a holdover from C.
>
>     This is something we've left room to consider in the future, but
>     feels
>     like "scope creep" at the present time.
>
>     > 2. I have an idea for a slightly different approach to the problem
>     > that may solve a few of the problems being proposed, and allows for
>     > imports, which would make it more useful for scripting use cases.
>
>     AFAICS, the spirit of what you're suggesting is to say that we can
>     treat
>     an instance initializer as a "main" method.
>
>     I would put this in the category of "clever" ideas, where you're
>     repurposing an existing language construct (instance initializers) to
>     avoid typing the characters "void main()".   Unfortunately, this
>     fails
>     the "on ramp leads gracefully into the highway" test; when the class
>     gets complicated enough to need to be a named class, the instance
>     initializer sticks out like a sore thumb, because the author
>     didn't want
>     an instance initializer, they wanted a main method.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20230417/ce145a50/attachment.htm>


More information about the amber-dev mailing list