Raw String Literals (RSL) - indent stripping of multi-line strings

Stephen Colebourne scolebourne at joda.org
Mon Apr 23 23:11:34 UTC 2018


On 23 April 2018 at 18:04, Jim Laskey <james.laskey at oracle.com> wrote:
> QUESTIONS:
> - Should the javac compiler remove incidental indentation at compile time?

If we accept the premise that RSLs indented with the code are common,
then stripping will indeed be very common and letting the compiler do
it is probably best. However, I struggle with the notion that a
stripped RSL is really the same language feature as a non-stripped
RSL.

>     - Should trailing whitespace be stripped?

No, that is an IDE problem.

> - What is the rule set used?
>     - Should the last line be a determining line?
>     - Should the first or last line be removed if blank?

I think there is a strong case for the stripping to be opt-in as follows:
- if an RSL opening delimiter is followed immediately by a new line,
then it is stripped (otherwise it is not stripped)
- the initial line of a stripped RSL is not part of the body of the RSL
- if a stripped RSL ends with a line consisting only of whitespace
before the delimiter, that line is not part of the body of the RSL
- all other intermediate lines are part of the body

Once the body to be stripped is determined, apply the rules:
- the determining lines are any non-blank lines
- on determining lines, identify the common prefix consisting of the
same whitespace character (prefix must be all spaces or all tabs)
- remove the common prefix

Thus:
- case a would not be stripped as there is text on the same line as
the opening delimiter.
- case b, d, e, f would be handled as proposed
- favour case d over case c, because the last line only contains
whitespace before the closing delimiter
- case g (below) has no trailing new line

    String g = `
........line one
........line two`;

Key differences are making stripping opt-in, and requiring that the
common prefix be of the same character. The effective cost of
excluding case a is one extra blank line (turning it into case b). The
key downside is that an RSL that should not be stripped beginning with
a new line must be left-formatted - but this seems similar to the
current edge case rules with delimiters. I personally find the opt-in
to be cleaner than always stripping.

Personally, I would consider using single delimiters for the
non-stripped and triple delimiters or more for stripped. YMMV.

Stephen


More information about the amber-dev mailing list