Two existing languages where multiline strings are used a lot are
Python and Ruby, and neither of them ignore the first leading newline.
Java doesn't support multiline strings but C# does (when using @
string literals) and doesn't ignore the first leading newline either.
Now I can definitively see why ignoring it can be a desirable thing
for many uses. But then there's this familiarity thing that has been
mentioned a number of times. I'd be curious to hear your thoughts
about the balance of the two.
Ignoring the first leading newline in multiline strings basically turns
var s = """
hellojed
""";
into
var s = """hellojed
""";
/Olov
Cheers,
Peter
Cheers,
Peter
/Olov
body = '''\<html><head></head>..</html>'''
Do you have a sense for how frequently the trailing newline leads to
poorly formatted strings in Python?
Cheers,
Peter
Do you have a sense for how frequently the trailing newline leads to
poorly formatted strings in Python?
HEADER = '''// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file// for details. All rights reserved. Use of this source code is governed by a// BSD-style license that can be found in the LICENSE file.// Generated by %s.'''
It's worth noting that both Ruby and Python have ways to create
multiline strings where the leading newline *is* ignored. Ruby has
Heredoc literals, like this:
$ irb
ruby-1.9.2-p180 :001 > s = <<END
ruby-1.9.2-p180 :002"> hellojed
ruby-1.9.2-p180 :003"> END
=> "hellojed\n"
ruby-1.9.2-p180 :004 >
Perl, PHP and Bash all support Heredoc as well.
In Python you can escape the initial newline to get a Heredoc-like effect.
I think ignoring the leading newline leads to better-looking code,
without begin confusing to users of other languages. Also, consider
the failure mode. Most of the time, people who expect a leading
newline to be part of the string won't actually want the newline.
They'll just put the beginning of the string right after the quotes,
and get what they expect, never knowing that it could have been nicely
formatted. I bet the number of people confused by the absence of a
newline they expected would be vanishingly small.
Colin
Given that, it would make me happy if it stripped leading (and trailing?) newlines, and also removed as many indentation characters from each line as there are before the opening '''. So this:return new View.html('''<div>Add or remove feeds in<a href="https://www.google.com/reader" target="_blank">Google Reader</a>'s "Subscriptions".Then come back here and click "Done" and we'll load your updatedlist of subscriptions.</div>''');Would yield a string that's exactly:<div>\n__Add or remove feeds in\n____Google Reader</a>'s "Subscriptions".\n__Then come back here and click "Done" and we'll load your updated\n__list of subscriptions.\n</div>But I don't have much of an opinion on this one way or the other.
I really like this approach too. Seems like ignoring leading newline
is here to stay for now (as clarified in
<http://code.google.com/p/dart/issues/detail?id=240#c5>) so I guess
dartc should mimic the current VM behavior. I'll file another bug for
that unless someone tells me it's not necessary.
My takeaway from this thread (thanks everyone for participating) is that
1) Ignoring leading newline is indeed useful and lends itself to more
beautifully formatted code.
2) The familiarity issue isn't that big of a deal since it is likely
that many programmers don't intuitively understand leading newline
semantics in other languages, or at least don't use it consistently.
/Olov