From: Stefan Weiss on
On 03/02/10 07:11, Thomas 'PointedEars' Lahn wrote:
> Stefan Weiss wrote:
>> I've always wondered why multi-line string literals
>> aren't possible in JavaScript. AFAICS, there's nothing ambiguous about
>> this syntax:
>>
>> var heredoc = "I am a multi-line
>> string; I end when the tokenizer
>> sees a double quote";
>
> Nothing ambiguous? To begin with, should that be the equivalent of
>
> var heredoc = "I am a multi-line string; I end when the tokenizer sees a
> double quote";
>
> or
>
> var heredoc = "I am a multi-linestring; I end when the tokenizersees a
> double quote";
>
> ? Besides, you forgot to consider `\"' in your description.

Fair enough, "unescaped double quote", then.

The example was meant to include all contained white space. Its result
would be "(...) multi-line\n string (...)", because it was indented.
"Nothing ambiguous" referred to the parser's theoretical ability to
continue strings literals over line breaks.

>> This is standard practice in many other programming languages.
>
> All other programming languages that I know to support this except PHP
> require special syntax there. So which "many other programming languages"
> are you referring to?

Some examples are Perl, PHP, Ruby, and Bash scripts. There are others.

>> The two usual workarounds are
>>
>> var str = "I wish\n"
>> + "I was\n"
>> + "a multi-line string";
>
> Joining an Array of strings on newlines appears to be easier to maintain:
>
> var str = [
> "I wish",
> "I was",
> "a multi-line string"
> ].join("\n");

I guess that's a matter of preference. I think the concatenation is more
readable. It's also faster, and concatenation of literals can be
optimized away by the parser.

>> var str = "I am the closest thing\
>> to multi-line strings that we can get\
>> with JavaScript";
>
> That is not equivalent, of course, as the leading spaces are part of the
> string value.

It's also different text, so of course they aren't equivalent. The
spaces are unimportant, I was talking about the line breaks.

>> I don't know how well supported the second version is.
>
> I have a fair idea. My tests indicate that this feature first specified
> in ECMAScript Edition 5 is supported since JavaScript 1.8.1 (at least),
> JScript 5.1.5010, V8 1.3 (at least), JavaScriptCore 525.13 (at least),
> Opera ECMAScript 10.10 (not before), and KJS 4.3.2 (at least).

I've never seen a browser where it didn't work, but I haven't done any
systematic testing.


--
stefan
From: Thomas 'PointedEars' Lahn on
Stefan Weiss wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Stefan Weiss wrote:
>>> I've always wondered why multi-line string literals
>>> aren't possible in JavaScript. AFAICS, there's nothing ambiguous about
>>> this syntax:
>>>
>>> var heredoc = "I am a multi-line
>>> string; I end when the tokenizer
>>> sees a double quote";
>>
>> Nothing ambiguous? To begin with, should that be the equivalent of
>>
>> var heredoc = "I am a multi-line string; I end when the tokenizer sees
>> a double quote";
>>
>> or
>>
>> var heredoc = "I am a multi-linestring; I end when the tokenizersees a
>> double quote";
>>
>> ? Besides, you forgot to consider `\"' in your description.
>
> Fair enough, "unescaped double quote", then.
>
> The example was meant to include all contained white space. Its result
> would be "(...) multi-line\n string (...)", because it was indented.

ACK. Sorry, trimming the leading spaces when quoting is a bug of my
newsreader that I should have been aware of.

>>> This is standard practice in many other programming languages.
>>
>> All other programming languages that I know to support this except PHP
>> require special syntax there. So which "many other programming
>> languages" are you referring to?
>
> Some examples are Perl, PHP, Ruby, and Bash scripts. There are others.

Perl and Ruby I do not know well enough. Bash (in fact, all Bourne-shell
compatible shells, if I am not mistaken) I know well, but I forgot about
them.

Still it remains to be seen if there are enough programming languages that
do not require special syntax to justify your "many". For example, it does
not apply to the following languages I know rather well: BASIC (and
variants), Pascal (variants, and derivates), C (variants, and derivates),
Tcl (and derivates), Java, and Python. (And maybe I forgot some.)

>>> The two usual workarounds are
>>>
>>> var str = "I wish\n"
>>> + "I was\n"
>>> + "a multi-line string";
>>
>> Joining an Array of strings on newlines appears to be easier to
>> maintain:
>>
>> var str = [
>> "I wish",
>> "I was",
>> "a multi-line string"
>> ].join("\n");
>
> I guess that's a matter of preference. I think the concatenation is more
> readable.

If you align `=' and `+', but you waste characters then and have to take
care not to forget a trailing "\n".

> It's also faster,

It is not generally faster.

> and concatenation of literals can be optimized away by the parser.

True.

>>> var str = "I am the closest thing\
>>> to multi-line strings that we can get\
>>> with JavaScript";
>>
>> That is not equivalent, of course, as the leading spaces are part of the
>> string value.
>
> It's also different text, so of course they aren't equivalent. The
> spaces are unimportant, I was talking about the line breaks.

But spaces are important in that to have equivalent value (regardless of
the words) you need to write

var str = "I am the closest thing\
to multi-line strings that we can get\
with JavaScript";

which is harder readable.

>>> I don't know how well supported the second version is.
>>
>> I have a fair idea. My tests indicate that this feature first specified
>> in ECMAScript Edition 5 is supported since JavaScript 1.8.1 (at least),
>> JScript 5.1.5010, V8 1.3 (at least), JavaScriptCore 525.13 (at least),
>> Opera ECMAScript 10.10 (not before), and KJS 4.3.2 (at least).
>
> I've never seen a browser where it didn't work,

It does not appear to work in Opera before version 10.10, so if what you
say is true, you must have never tested with Opera (10.10 is currently the
latest version).

In which browsers (other than supporting the implementations above) do you
remember this to have worked, i.e. that

var s = "foo\
bar";

would be equivalent to

var s = "foobar";

?


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(a)news.demon.co.uk> (2004)
From: Thomas 'PointedEars' Lahn on
John G Harris wrote:

> Stefan Weiss wrote:
>> I've always wondered why multi-line string literals
>> aren't possible in JavaScript. AFAICS, there's nothing ambiguous about
>> this syntax:
>>
>> var heredoc = "I am a multi-line
>> string; I end when the tokenizer
>> sees a double quote";
>>
>> This is standard practice in many other programming languages.
> <snip>
>
> In C++ you would write
>
> var heredoc = "I am a multi-line "
> "string; I end when the tokenizer "
> "sees a double quote";

No, that is not C++ code.

> or
>
> var heredoc = "I am a multi-line\n"
> "string; I end when the tokenizer\n"
> "sees a double quote";

Nor is this.

Perhaps you meant

std::string heredoc = "I am a multi-line"
" string; I end when the tokenizer"
" sees a double quote";

> Note the double quotes at both ends of the string parts. This way, you
> know exactly which whitespace chars belong to the string parts and which
> are just there for layout.

And by contrast it requires *special syntax*, so Stefan's assumption does
_not_ apply here. Thank you for the counter-example, albeit a bogus one.


PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
From: John G Harris on
On Wed, 3 Feb 2010 at 18:51:45, in comp.lang.javascript, Thomas
'PointedEars' Lahn wrote:
>John G Harris wrote:
>
>> Stefan Weiss wrote:
>>> I've always wondered why multi-line string literals
>>> aren't possible in JavaScript. AFAICS, there's nothing ambiguous about
>>> this syntax:
>>>
>>> var heredoc = "I am a multi-line
>>> string; I end when the tokenizer
>>> sees a double quote";
>>>
>>> This is standard practice in many other programming languages.
>> <snip>
>>
>> In C++ you would write
>>
>> var heredoc = "I am a multi-line "
>> "string; I end when the tokenizer "
>> "sees a double quote";
>
>No, that is not C++ code.

You time-waster. It's perfectly valid C++ code. var is obviously a
type-name, for a type having a constructor with the signature
var(const char * )


>> or
>>
>> var heredoc = "I am a multi-line\n"
>> "string; I end when the tokenizer\n"
>> "sees a double quote";
>
>Nor is this.

ditto.


>Perhaps you meant
>
> std::string heredoc = "I am a multi-line"
> " string; I end when the tokenizer"
> " sees a double quote";

That's another example, but not the one I was using.


>> Note the double quotes at both ends of the string parts. This way, you
>> know exactly which whitespace chars belong to the string parts and which
>> are just there for layout.
>
>And by contrast it requires *special syntax*, so Stefan's assumption does
>_not_ apply here. Thank you for the counter-example, albeit a bogus one.

But Stefan was suggesting additional special syntax!

Score adjusted.

John
--
John Harris
From: Garrett Smith on
John G Harris wrote:
> On Wed, 3 Feb 2010 at 18:51:45, in comp.lang.javascript, Thomas
> 'PointedEars' Lahn wrote:
>> John G Harris wrote:
>>
>>> Stefan Weiss wrote:

[...]
> But Stefan was suggesting additional special syntax!
>
> Score adjusted.
>
LOL
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/