Suggestions to triple quoted strings proposal

classic Classic list List threaded Threaded
57 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Suggestions to triple quoted strings proposal

Stepan Koltsov-2
Hi,

I've read proposal of triple quoted strings at

http://developer.mozilla.org/es4/proposals/triple_quotes.html

And I have two suggestions. I've sent them to Brendian Eich, and he
redirected me to this mailing list.

I suggest to change interpretation of triple quoted string.

First,

I suggest to ignore first character of string if it is newline. So statement

messageTemplate = """
Hello,
I've just...
"""

should be equivalent to

messageTemplate = "Hello,\nI've just...\n"

, but not

messageTemplate = "\nHello,\nI've just...\n".

And second: to ignore same number of spaces as number of spaces in
the line where triple quoted string started. Example:

function query() {
    return """
    SELECT * FROM people
    WHERE date_created > ? AND disabled = 'N'
    """
}

should be equivalent to

function query() {
    return "SELECT * FROM people\nWHERE date_created > ? AND disabled = 'N'\n";
}

but not as now:

function query() {
    return "    SELECT * FROM people\n    WHERE date_created > ? AND
disabled = 'N'\n";
}

I think that these improvements would make triple quoted string more
useful and easier. Hope, these suggestions will be added to proposal.

--
Stepan Koltsov

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Brendan Eich-2
On Dec 12, 2006, at 11:24 AM, Stepan Koltsov wrote:

> First,
>
> I suggest to ignore first character of string if it is newline. So  
> statement
>
> messageTemplate = """
> Hello,
> I've just...
> """
>
> should be equivalent to
>
> messageTemplate = "Hello,\nI've just...\n"
>
> , but not
>
> messageTemplate = "\nHello,\nI've just...\n".

This is not what Python does:

 >>> s = """
...   hi
...   there
... """
 >>> s
'\n  hi\n  there\n'

> And second: to ignore same number of spaces as number of spaces in
> the line where triple quoted string started. Example:
>
> function query() {
>    return """
>    SELECT * FROM people
>    WHERE date_created > ? AND disabled = 'N'
>    """
> }
>
> should be equivalent to
>
> function query() {
>    return "SELECT * FROM people\nWHERE date_created > ? AND  
> disabled = 'N'\n";
> }
>
> but not as now:
>
> function query() {
>    return "    SELECT * FROM people\n    WHERE date_created > ? AND
> disabled = 'N'\n";
> }
>
> I think that these improvements would make triple quoted string more
> useful and easier. Hope, these suggestions will be added to proposal.

This is a different kind of quoting than what " or """ means in JS or  
Python.  It seems to me to warrant a different syntax, if it's worth  
doing at all.  The counterargument besides "don't diverge from  
Python" is: "just don't indent or add extra newlines you don't want  
in the literal."

Literal means verbatim, quotes mean verbatim. Stripping leading  
uniform indentation (or did you mean any leading whitespace, even if  
the first line has three chars and the second has seven, etc.) is not  
verbatim.  Chomping leading and trailing newlines is not verbatim.  
The "KISS" principle favors the minimal, What you typed is what you  
get (WYTIWYG? sorry) approach.

My two cents, anyway.

/be


Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Jeff Walden-3
Brendan Eich wrote:

> On Dec 12, 2006, at 11:24 AM, Stepan Koltsov wrote:
>> And second: to ignore same number of spaces as number of spaces in
>> the line where triple quoted string started. Example:
>>
>> function query() {
>>    return """
>>    SELECT * FROM people
>>    WHERE date_created > ? AND disabled = 'N'
>>    """
>> }
>>
>> should be equivalent to
>>
>> function query() {
>>    return "SELECT * FROM people\nWHERE date_created > ? AND disabled =
>> 'N'\n";
>> }
>>
>> but not as now:
>>
>> function query() {
>>    return "    SELECT * FROM people\n    WHERE date_created > ? AND
>> disabled = 'N'\n";
>> }
>
> This is a different kind of quoting than what " or """ means in JS or
> Python.  It seems to me to warrant a different syntax, if it's worth
> doing at all.  The counterargument besides "don't diverge from Python"
> is: "just don't indent or add extra newlines you don't want in the
> literal."

A further note on this: what you suggest here is the "post-processing" behavior of docstrings in Python.  From <http://docs.python.org/tut/node6.html#SECTION006760000000000000000>, "The Python parser does not strip indentation from multi-line string literals in Python, so tools that process documentation have to strip indentation if desired."  To an extent I agree that it would be convenient if leading whitespace were stripped from such string literals during actual program execution (and not just by docs post-processors), but I suspect you weren't aware (as I wasn't before looking this up!) that this step was a post-processing step, and that the actual string retained its indentation.

Jeff

--
Rediscover the Web!
http://snurl.com/get_firefox

Reclaim Your Inbox!
http://snurl.com/get_thunderbird

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Peter Hall-2
In reply to this post by Stepan Koltsov-2
> I suggest to ignore first character of string if it is newline.

Can you explain why this would be useful?

If you ignore the first new line, should you also ignore all leading
new lines? It's not obvious how it would behave in that situation,
which makes it immediately "harder" - but that could be just because I
don't understand the use-case. Also, if you actually wanted to include
leading newlines you'd then have to use escape sequences which would
seem to defeat the whole point of """.

Assuming that there are some really convincing use-cases, perhaps
something along the lines of literal regexp options syntax could work.
e.g:

query = """
   SELECT * FROM people
   WHERE date_created > ? AND disabled = 'N'
   """n;

// n signifies ignore leading white on each line


But I'm dubious about how useful that is, compared to the extra
complexity, and I think Brendan is right about keeping it simple
(though I can't see "WYTIWYG" catching on ;-))


Btw is there a discussion somewhere, deciding on triple-quotes in
preference to heredoc syntax?

Peter

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Peter Hall-2
Another quick comment. With the addition of E4X, you can use
triple-nested multiline strings with this slightly mucky workaround:

                var q:String = <>
                "Som'e' "
                multiline text
                </>.toXMLString();

In the AS3 implementation, this is equivalent to:

var q:String = "\"Som'e' \"\n\t\t'mult'iline text";


Which is interesting for comparison, as it ignores all leading but not
subsequent white.

Peter

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Jeff Walden-3
Peter Hall wrote:
> Which is interesting for comparison, as it ignores all leading but not
> subsequent white.

This is due to XML.prettyPrinting being true, I suspect; I'd check if I had more free time right now.  Also, unless you drop to entities you can't embed {, }, or < in the string this way.

Jeff

--
Rediscover the Web!
http://snurl.com/get_firefox

Reclaim Your Inbox!
http://snurl.com/get_thunderbird

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Brendan Eich-2
On Dec 12, 2006, at 2:14 PM, Jeff Walden wrote:

> Peter Hall wrote:
>> Which is interesting for comparison, as it ignores all leading but  
>> not
>> subsequent white.
>
> This is due to XML.prettyPrinting being true, I suspect; I'd check  
> if I had more free time right now.  Also, unless you drop to  
> entities you can't embed {, }, or < in the string this way.

Right.

BTW, this works in SpiderMonkey (in Firefox):

var q = String( <>
                 "Som'e' "
                 multiline text
                 </>.toXMLString() );
alert(uneval(q));

/be



Reply | Threaded
Open this post in threaded view
|

Re: Re: Suggestions to triple quoted strings proposal

Stepan Koltsov-2
In reply to this post by Peter Hall-2
On 12/13/06, Peter Hall <[hidden email]> wrote:
> > I suggest to ignore first character of string if it is newline.
>
> Can you explain why this would be useful?

I suppose we understand triple-quoted string (TQS) literals
differently. Possibly you think about TQS as about ability to insert
raw binary data into the program (WYTIWYG). I think TQS allows easily
embed good old plain texts. Plain text is a sequence of lines joined
by newline. "What you see is what you get" works here :) Look at the
code:

===
function query() {
    return """
    SELECT 15
    FROM dual
    """
}

What do you see inside of triple quotes? ;-)

I see two lines of text. First line is "SELECT 15". Second line is
"FROM dual". So I think function should return text consisted from
these two line joined by newline-separator.

About use cases. You may wish to inline mail message template inside
your program.

===
class SuperDuperLogger {
    // ...
    function mailThatDataIsLost(err, users) {
        for (user in users) {
            var msgTmpl = """
            Dear @USER@,

            Something bad happened: all your data is lost:

            @TEXT@

            S.Y., Robot.
            """
            sendMessage(user.email,
                msgTmpl.replace("@USER@", user.name).replace("@ERR@", text));
        }
    }
}
===

This is snippet of code is to send letter if error happened. If we
have Python-like multiline strings behaviour, and don't want to
postprocess message template (explicitly strip spaces; we don't), we
have to write same piece of code such way:

===
class SuperDuperLogger {
    // ...
    function mailAboutSeriousError(err) {
        for (user in users) {
            var msgTmpl = """Dear @USER@,

Something bad happened: all your data lost:

@TEXT@

S.Y., robot.
            """
            sendMessage(msgTmpl.replace("@USER@", user).replace("@ERR@", text));
        }
    }
}
===

It looks really bad. Text breaks loop, function and class body. We do
not see the place of the beginning of message template.

My behaviour looks better in any situation where you want to get plain
text. Another example: part of code generator:

function writeHead() {
    writer.write("""
    /*
     * WARNING! This file is machine generated! Do not edit.
     *
     * Generated by Mega-Generator 2006 Enterprise.
     */
    """)
}

> If you ignore the first new line, should you also ignore all leading
> new lines?

No, because line after """ is first line of _text_.

> It's not obvious how it would behave in that situation,
> which makes it immediately "harder" - but that could be just because I
> don't understand the use-case. Also, if you actually wanted to include
> leading newlines you'd then have to use escape sequences which would
> seem to defeat the whole point of """.

Please explain better, I do not understand.

> Assuming that there are some really convincing use-cases, perhaps
> something along the lines of literal regexp options syntax could work.
> e.g:
>
> query = """
>    SELECT * FROM people
>    WHERE date_created > ? AND disabled = 'N'
>    """n;
>
> // n signifies ignore leading white on each line
>
> But I'm dubious about how useful that is, compared to the extra
> complexity, and I think Brendan is right about keeping it simple
> (though I can't see "WYTIWYG" catching on ;-))

Suffix "n" is unnecessary complexity. There should be either original
semantics or mine.

I'd ever prohibited any text after first """ to avoid misunderstanding.

I know, that my suggestion makes rules more complex. I don't think
this leads to programming errors, because described behaviour is
intuitive.

--
Stepan

Reply | Threaded
Open this post in threaded view
|

Re: Re: Suggestions to triple quoted strings proposal

Johan Sundström
> > If you ignore the first new line, should you also ignore all leading
> > new lines?
>
> No, because line after """ is first line of _text_.
>
> > It's not obvious how it would behave in that situation,
> > which makes it immediately "harder" - but that could be just because I
> > don't understand the use-case. Also, if you actually wanted to include
> > leading newlines you'd then have to use escape sequences which would
> > seem to defeat the whole point of """.
>
> Please explain better, I do not understand.

Assuming that

A) all (and not just the first one, as you suggest) initial newlines
are ignored, and
B) you want to produce a string with leading newlines using the """ syntax,

you would have to use something like """\n\n\nThe string that follows
our leading newlines""".


I personally find """ (and/or ''' -- or is maybe only """ under
discussion?) most unreadable -- how many quotes was that?

It's especially difficult, granted that " and ' characters look much
the same in a forest of quotes, and that i e "'" is a valid
one-character string. At least I can't easily tell the difference,
especially when scanning code, without the aid of some Christmas tree
mode (it gets even worse when discussing code in a non-monospaced
environment). (If the string were to contain actual code, indented
just like the surrounding code too, it gets increasingly worse.)

I would be glad either not to see the change at all, or to see some
alternate syntax, perhaps the more character conservarive #"(string
including literal newlines here" syntax (used by Pike).

--
 / Johan Sundström, http://ecmanaut.blogspot.com/


Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Bob Ippolito
In reply to this post by Stepan Koltsov-2
On 12/13/06, Stepan Koltsov <[hidden email]> wrote:

> Hi,
>
> I've read proposal of triple quoted strings at
>
> http://developer.mozilla.org/es4/proposals/triple_quotes.html
>
> And I have two suggestions. I've sent them to Brendian Eich, and he
> redirected me to this mailing list.
>
> I suggest to change interpretation of triple quoted string.
>
> First,
>
> I suggest to ignore first character of string if it is newline. So statement
>
> messageTemplate = """
> Hello,
> I've just...
> """
>
> should be equivalent to
>
> messageTemplate = "Hello,\nI've just...\n"
>
> , but not
>
> messageTemplate = "\nHello,\nI've just...\n".
>
> And second: to ignore same number of spaces as number of spaces in
> the line where triple quoted string started. Example:
>
> function query() {
>     return """
>     SELECT * FROM people
>     WHERE date_created > ? AND disabled = 'N'
>     """
> }
>
> should be equivalent to
>
> function query() {
>     return "SELECT * FROM people\nWHERE date_created > ? AND disabled = 'N'\n";
> }
>
> but not as now:
>
> function query() {
>     return "    SELECT * FROM people\n    WHERE date_created > ? AND
> disabled = 'N'\n";
> }
>
> I think that these improvements would make triple quoted string more
> useful and easier. Hope, these suggestions will be added to proposal.

Eh. String should just grow a method to do that. Literals are literals
and should be treated as such.

In Python you do that with the textwrap module.

>>> textwrap.dedent("\n    foo\n    bar\n        baz")
'\nfoo\nbar\n    baz'

The leading newline needs to be stripped manually (e.g. lstrip('\n'))
or more commonly the opening triple quote is followed by a line
continuation backslash.

-bob

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

zwetan-3
In reply to this post by Brendan Eich-2
Hello all,
here my two cents from a user point of view

- """, why use triple we don't even have double ?
  I would prefer some before-quote-parameter
  as in C# using @"some text here"
  (anyother parameter than @ is fine)

- I like TQS because it allow to write multiline
  without juggling with string concatenation to keep it readable

  """
  hello world
  here another line
  and another line
  """

  is much more readable in source than
  "hello world\nhere another line\nand another line"
   or
   somevar   = "hello world\n"
   somevar += "here another line\n"
   somevar += "and another line"

  so imho the goal of using """ is to be able to write readable
multiline of text
  IN the source code

  but as people indent their code

          """
      ....hello world
      ....here another line
      ....and another line
          """
      (the dots being spaces)

     I would find usefull to have an option (even if it's not pythonesque)
     that strip the 4 spaces on the left automatically
     so if I my lines are aligned they stay readable
     (maybe I'm not super clear here)

    my ideal notation would be something like this

          @-4"
      ....hello world
      ....here another line
      ....and another line
          "

    @ -> we are in multiline
    -4 strip four spaces on the left
    (+4 or 4 could be used to add four spaces on the left)

    resulting in this
    "hello world\nhere another line\nand another line"

- one last option I would like to see also is to be able
  to define the line separator sequence
  "\n" could be the default
  but being able to define "\r", "\r\n" could be helpfull too

OR

Keep the TQS as simple as possible,
no special string rules to remember, all is verbatim

but add a "prettyPrinting" method to the String class
in order to strip spaces/whitespaces at the start of lines AND
replacing the line separator at end of lines

as the String class is final, to have such method native
would be inho more efficient

zwetan

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Bob Ippolito
On 12/13/06, zwetan <[hidden email]> wrote:

> Hello all,
> here my two cents from a user point of view
>
> - """, why use triple we don't even have double ?
>   I would prefer some before-quote-parameter
>   as in C# using @"some text here"
>   (anyother parameter than @ is fine)
>
> - I like TQS because it allow to write multiline
>   without juggling with string concatenation to keep it readable
>
>   """

Double quotes would probably be syntactically ambiguous. They look
exactly like an empty string token...

Even if you wormed around that, one of the most common use cases for
triple quoted strings (in Python) is to facilitate documentation and
doctests. Triple quotes can represent any syntactically valid code
without using escapes (except of course, another set of triple
quotes). If it were double quotes, empty string literals would have to
be escaped (or '' would have to be used).

Decorating the quote with some sigil doesn't make multi-line easier,
nor does it get you around the escaping problem. Totally different use
case with no feature overlap.

-bob

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

zwetan-3
On 12/13/06, Bob Ippolito <[hidden email]> wrote:

> Double quotes would probably be syntactically ambiguous. They look
> exactly like an empty string token...
>
> Even if you wormed around that, one of the most common use cases for
> triple quoted strings (in Python) is to facilitate documentation and
> doctests. Triple quotes can represent any syntactically valid code
> without using escapes (except of course, another set of triple
> quotes). If it were double quotes, empty string literals would have to
> be escaped (or '' would have to be used).
>
> Decorating the quote with some sigil doesn't make multi-line easier,
> nor does it get you around the escaping problem. Totally different use
> case with no feature overlap.
>

my concern is to have the multiline readable in the source code,
if I don't need it readable I don't really need multiline and TQS, I just use
a String, so now maybe the problem is the different point of view
about what is "easier"

I'm personally not interested in carrying the documentation of code
inside the code itself and I can bet a lot do not pursue this,
so I don't think the most common use case
for TQS in ES4 gonna be the same as in Python

but still TQS to define big chunk of readable text into the code
can be very usefull, even if not totally Python identical (and I do
like how Python do things, not bashing here).

zwetan

Reply | Threaded
Open this post in threaded view
|

Re: Re: Suggestions to triple quoted strings proposal

Peter Hall-2
In reply to this post by Stepan Koltsov-2
> ===
> function query() {
>     return """
>     SELECT 15
>     FROM dual
>     """
> }
>
> What do you see inside of triple quotes? ;-)
>
> I see two lines of text. First line is "SELECT 15". Second line is
> "FROM dual". So I think function should return text consisted from
> these two line joined by newline-separator.
>


It doesn't seem particularly arduous to do:

function query() {
   return
   """SELECT 15
   FROM dual
   """
}


Peter

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Dave Herman-2
> It doesn't seem particularly arduous to do:
>
> function query() {
>   return
>   """SELECT 15
>   FROM dual
>   """
> }

I haven't been following this thread closely, but the above will not
parse; you aren't allowed to move the argument to a return to the next
line. The parser will parse that as a return with no arguments.

Dave

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Peter Hall-2
Whose silly idea was automatic semi-colon insertion anyway! ;-)

Peter


On 12/13/06, Dave Herman <[hidden email]> wrote:

> > It doesn't seem particularly arduous to do:
> >
> > function query() {
> >   return
> >   """SELECT 15
> >   FROM dual
> >   """
> > }
>
> I haven't been following this thread closely, but the above will not
> parse; you aren't allowed to move the argument to a return to the next
> line. The parser will parse that as a return with no arguments.
>
> Dave
>

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

P T Withington
In reply to this post by Dave Herman-2
On 2006-12-13, at 10:13 EST, Dave Herman wrote:

>> It doesn't seem particularly arduous to do:
>> function query() {
>>   return
>>   """SELECT 15
>>   FROM dual
>>   """
>> }
>
> I haven't been following this thread closely, but the above will  
> not parse; you aren't allowed to move the argument to a return to  
> the next line. The parser will parse that as a return with no  
> arguments.

One of the biggest traps of implicit `;`s.  Bitten me too many  
times!  But you _could_ say:

function query() {
   return (
"""SELECT 15
FROM dual
""")
}

Since I'm here, I'll just toss out the Dylan way, which is to have  
adjacent strings concatenate:

function query() {
   return (
    "SELECT 15"
    " FROM dual"
   )
}


Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Bob Ippolito
On 12/14/06, P T Withington <[hidden email]> wrote:

> On 2006-12-13, at 10:13 EST, Dave Herman wrote:
>
> >> It doesn't seem particularly arduous to do:
> >> function query() {
> >>   return
> >>   """SELECT 15
> >>   FROM dual
> >>   """
> >> }
> >
> > I haven't been following this thread closely, but the above will
> > not parse; you aren't allowed to move the argument to a return to
> > the next line. The parser will parse that as a return with no
> > arguments.
>
> One of the biggest traps of implicit `;`s.  Bitten me too many
> times!  But you _could_ say:
>
> function query() {
>    return (
> """SELECT 15
> FROM dual
> """)
> }
>
> Since I'm here, I'll just toss out the Dylan way, which is to have
> adjacent strings concatenate:
>
> function query() {
>    return (
>     "SELECT 15"
>     " FROM dual"
>    )
> }

In many cases you probably want to preserve the newlines though, where
adjacent string concatenation makes it look like this:

return (
    "while True:\n"
    "    pass\n"
    );

-bob

Reply | Threaded
Open this post in threaded view
|

Re: Suggestions to triple quoted strings proposal

Brendan Eich-2
In reply to this post by zwetan-3
On Dec 13, 2006, at 2:35 AM, zwetan wrote:

> OR
>
> Keep the TQS as simple as possible,
> no special string rules to remember, all is verbatim

Sold! ;-)

> but add a "prettyPrinting" method to the String class
> in order to strip spaces/whitespaces at the start of lines AND
> replacing the line separator at end of lines
>
> as the String class is final, to have such method native
> would be inho more efficient

Please comment on http://developer.mozilla.org/es4/proposals/ 
string.html -- it is a late breaking and small "leaf" proposal (just  
standard library we don't want everyone to have to re-implement and  
deploy ad nauseum), and it is not done yet.

/be


Reply | Threaded
Open this post in threaded view
|

Re: Re: Suggestions to triple quoted strings proposal

Stepan Koltsov-2
Brendan, or anybody else who wants multiline strings should to behave
like in Python,

Could you please write complex-enough example of code with TQS? In
that example string constant should be declared inside method inside
class. There is no good example at
http://developer.mozilla.org/es4/proposals/triple_quotes.html .

I used to write in Python, I hated its """ behaviour. I asked people
who use Python, and they generally agreed with me.

I'm afraid, that if you keep TQS "simple", they won't be very usable:
in 99% cases users will be forced to manually strip spaces and leading
newline. In 1% cases string constant will be defined outside block, or
amount of spaces will not matter.

I have no other arguments :)

--
Stepan

123