[swift-evolution] (no subject)
John Siracusa
siracusa at gmail.com
Fri Dec 11 10:52:38 CST 2015
Chris Lattner wrote:
> When introducing a feature like this, I think it would be useful to survey
> a range of popular languages (and yes, even perl ;-) to understand what
> facilities they provide and why (i.e. what problems they are solving) and
> synthesize a good swift design that can solve the same problems with a
> hopefully simple approach.
Travis Tilley wrote:
> Perl and Erlang are unique in that valid code in either language looks
> essentially like line noise. I'd rather take inspiration from languages
> like ruby, python, and elixir.
Jokes aside, the ability to choose delimiters for strings and other
language constructs that surround some value is a huge boon to code
readability.
For example, RegExp literals in JavaScript:
var regex = /^\/usr\/local\//; // gross
An even simpler example, which applies to many languages: a string literal
that contains all of your possible string delimiters within it. This is not
an exotic thing in English.
message = "\"I don't like this,\" she said."; // nope
message = '"I don\'t like this," she said.'; // still nope
Then, of course, there's your escape character itself:
escapes = "Some escapes: \\n, \\t, \\a"; // sigh
There are many time-tested solutions to these syntactic/cosmetic problems.
* Different delimiters with different interpolation rules (e.g., single
quotes not honoring any backslash escapes and not doing variable
interpolation)
* Matched-pair delimiters that don't require anything to be escaped as long
as the delimiters are absent or matched within the string. (These alone
solve a huge range of problems.)
* Heredocs for long literals where you get to pick the end token.
* Heredocs modified by delimiters around the end token to control
interpolation within the long literal.
Which language looks like line noise now?
$messasge = q("I can't believe how nice this is," she said (quietly).);
$regex = qr(^/usr/local/);
$escapes = 'Some escapes: \n, \t, \a';
My take: once you use a language where you pretty much never have to
backslash-escape a character you can easily type to get it into a string,
it's really hard to go back.
-John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151211/828d3f48/attachment.html>
More information about the swift-evolution
mailing list