String reboot - (1a) incidental whitespace
Jim Laskey
james.laskey at oracle.com
Thu Apr 18 12:24:23 UTC 2019
I see value in this notion. Early on in the design of RSL we had a trimMargin equivalent defined in String. John suggested some modifications which led to a Swiss Army trimMargin which could handle everything you suggest and much more. In the end, what changed our minds about providing trimMargin in the JDK was the result of the RSL SurveyMonkey, indicating that most developers wanted a clutter free, low maintenance literal. Realizing that we would never please everyone, we fell back on the assumption that most were good with the basic literal and that customized align/trim could be handled by String::transform.
Cheers,
-- Jim
> On Apr 17, 2019, at 5:07 PM, Guy Steele <guy.steele at oracle.com> wrote:
>
> Okay, for this message I abandon rectangles and adopt Version 2 of the algorithm, as described by Brian (see below): a trailing blank line before the final delimiter is included in computing the leading whitespace prefix.
>
> Now I return to an idea we’ve seen before:
>
>> From: Tagir Valeev <amaembo at gmail.com <mailto:amaembo at gmail.com>>
>> Date: January 27, 2018 at 3:23:31 AM EST
>>
>> String html = `<html>
>> | <head>
>> | <title>Message</title>
>> | </head>
>> | <body>`.trimMargin();
>
>
> Here Tagir used pipe characters to indicate the intended left-hand margin of the embedded material, and a trimMargin() method to remove them (and preceding whitespace on the line).
>
> So: what if we allow use of that format, but instead of pipes, use double quotes?
>
> String s = “””
> “ I am
> “ a block of text
> “””;
>
> This gives a sort of “partial rectangle” that looks nice, is semi-easy to edit, and works okay with proportional fonts (you may not get good alignment with the leading delimiter, but you can always get perfect alignment with the trailing delimiter). However, functionally it really buys you nothing over what the indentation of the trailing delimiter tells you. So you can use it or not.
>
> Where this option shines is when you really don’t want that trailing newline, but do want specific indentation and don’t want to use a method call .indent(4):
>
> String s = “””
> “ I am
> “ a block of text“””;
>
> So for this design I would suggest adding a rule that until you hit that trailing delimiter, either every line DOES have a leading “, or every line does NOT have a leading “ (error if the two styles are mixed).
>
> If you want to use the style without leading “ but some line of content begins with “, then you have to escape it:
>
> String s = “””
> \"I am
> a block of text"
> “””;
>
> If you really don’t like using an escape (for one thing, it makes the relative indentation of content lines less clear), then use the other style:
>
> String s = “””
> " "I am
> " a block of text"
> “””;
>
> You have choices. I’m not entirely sure I like such a design, but I’m putting it out there for contemplation.
>
>
>> On Apr 17, 2019, at 2:40 PM, Brian Goetz <brian.goetz at oracle.com <mailto:brian.goetz at oracle.com>> wrote:
>>
>> Where this makes a difference is:
>>
>> String s = “””
>> I am
>> a block of text
>> “””;
>>
>> Under Version 1, we would get:
>>
>> I am
>> a block of text
>>
>> Under version 2, we would get
>>
>> I am
>> a block of text
>>
>> Under version 1, if we wanted the latter answer, we would have to do .indent(4) or something like that.
>>
>>
>>> On Apr 17, 2019, at 2:33 PM, Brian Goetz <brian.goetz at oracle.com <mailto:brian.goetz at oracle.com>> wrote:
>>>
>>>>
>>>> I'm sorry I'm not completely caught up on this discussion yet and may have missed something. But I'm confused what the alternative to using the closing delimiter position is. You certainly don't want to magically use the column position of the *opening* delimiter in this example! That is *definitely* incidental, as it depends on what the `query` variable got renamed to later by some refactoring tool. Variable renames shouldn't change program behavior.
>>>>
>>>
>>> Version 1 of the algorithm is:
>>> - Strip leading and trailing blanks (perhaps limited to one each only)
>>> - Compute the maximal common whitespace prefix of the remaining lines, and trim that off each line
>>> - If a blank last line was trimmed, add back a newline
>>>
>>> Version 2 of the algorithm — the “significant closing delimiter” version — is:
>>> - Strip leading and trailing blanks (perhaps limited to one each only)
>>> - Compute the maximal common whitespace prefix of the remaining lines, _including the stripped trailing blank line from above, if any_, and trim that off
>>> - If a blank last line was trimmed, add back a newline
>>>
>>> In other words, the difference is wether we strip off the trailing blank before or after we compute the leading whitespace prefix. In neither case do we use scanner position.
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/amber-spec-experts/attachments/20190418/e5299f8c/attachment.html>
More information about the amber-spec-experts
mailing list