Will Java ever allow comments to nest?

Pavel Rappo pavel.rappo at oracle.com
Wed Jul 22 14:42:30 UTC 2020


Roger,

No syntax based on block comments (/* ... */) will ever work. The JLS is very clear about that: whenever the compiler sees a */ in a block comment, the comment ends.

The only practical solution here is to allow _an alternative_ doc-comment syntax based on end-of-line comments (//). Now, we can bikeshed on a form of the markers: for example, whether it's going to be

    ///
    // ...
    // The result of {@code "/home/".matches(".*/")} is {@code true}.
    // ...
    // @returns ...
    ///

or

    /// ...
    /// The result of {@code "/home/".matches(".*/")} is {@code true}.
    /// ...
    /// @returns ...

or something like

    //* ...
    //* The result of {@code "/home/".matches(".*/")} is {@code true}.
    //* ...
    //* @returns ...

But `//` is the only practical way to go.

-Pavel

> On 22 Jul 2020, at 15:02, Roger Riggs <Roger.Riggs at oracle.com> wrote:
> 
> Hi,
> 
> It looks a lot like the quotes within quotes problem that has existed in shells forever and
> was addressed in text blocks with a different outer delimiter.
> 
> The kind of encoding below "*/" is error prone and makes the developer encode the real intent.
> There's got to be a better way, even if it takes a new kind of doc comment delimiter for the outer.
> It will be a bigger change for javac and javadoc but makes it possible to redefine/reinterpret the delimiters.
> Maybe you can do something with:
> 
>   /***
>        /**
>         * ...
>         */
>   ***/
> 
> 
> 
> Regards, Roger
> 
> On 7/7/20 6:40 AM, Johannes Kuhn wrote:
>> This is a source incompatible change.
>> 
>> It would break /* inside comments:
>> 
>>     /**
>>      * Determines whether or not the tokenizer recognizes C-style comments.
>>      * If the flag argument is {@code true}, this stream tokenizer
>>      * recognizes C-style comments. All text between successive
>>      * occurrences of {@code /*} and <code>*/</code> are discarded.
>>      * <p>
>>      * If the flag argument is {@code false}, then C-style comments
>>      * are not treated specially.
>>      *
>>      * @param   flag   {@code true} indicates to recognize and ignore
>>      *                 C-style comments.
>>      */
>> 
>> That's the javadoc of StreamTokenizer.slashStarComments(boolean).
>> With your proposed change, the javadoc comment would not be closed at the end.
>> 
>> - Johannes
>> 
> 



More information about the discuss mailing list