Alignment algorithm (was: Wrapping up the first two courses)
Elias N Vasylenko
elias at vasylenko.uk
Tue Apr 23 20:19:17 UTC 2019
On Tue, 2019-04-23 at 10:57 -0400, Brian Goetz wrote:
> > > I think we should reconsider whether a non-blank first line means
> > > that we should consider any indentation on the first line
> > > too. This
> > > has the likely-beneficial side-effect that having a non-blank
> > > character immediately following the """ effectively means "no
> > > stripping."
> > I would go the other way and say that any non-blank first line
> > should
> > mean no stripping at all. A non-blank first line will always have
> > at
> > least an extra three characters of indentation, so won't line up
> > with
> > the rest of the string. (Ignoring some questionable (ab)use of 4+
> > width
> > tabs.)
> This is worth considering — but as I’ve said before, this can’t be
> the only opt-out.
I agree! There needs to be a way to opt out without losing the leading
> One way to unify these is some sort of auto-stripped escape that does
> not count as whitespace.
Ah, well I had assumed that the auto-alignment would be applied to the
string *after* processing escape sequences, in which case a self-
deleting escape sequence wouldn't be visible to it. But on reflection I
think that was probably the wrong assumption.
Applying auto-alignment before processing escapes is a more faithful
expression of the principle that we're dealing with the embedding of
the string in 2D space in the source file.
> > Non-empty first lines would still be useful as an opt-out. And I'd
> > suggest a convention of using this opt-out by escaping the leading
> > newline, thus making the first line effectively non-empty without
> > messing up alignment in the source:
> > `
> > String s = """\
> > line 1
> > line 2
> > """;
> > `
> I would prefer to treat \<newline> as an escape that means “eat the
> newline”, as it is in other languages, as in the example John posted
> last week. But in that interpretation, you get the same effect,
> because the above is equavlient to
> > String s = """line 1
> > line 2
> > """;
> Or maybe that’s what you were suggesting?
Yes that was the idea! Or rather, that it would be equivalent to:
String s = """ line 1
I didn't notice it had already been suggested. But if indentation
stripping is applied before escaping is processed then it doesn't work
like that anyway. More exactly, it does still work, but for a different
reason. As does the following ...
String s = """\n\
... for when we want to opt out without losing the leading newline. In
this case I don't see the need for a new auto-stripped escape.
But in any case I believe the justification for disabling auto-
alignment when there is a non-empty first line stands for itself,
regardless of how auto-alignment interacts with escapes.
More information about the amber-spec-observers