Suggestions to triple quoted strings proposal

Stepan Koltsov stepan.koltsov at gmail.com
Tue Dec 12 16:51:22 PST 2006


On 12/13/06, Peter Hall <peterjoel at gmail.com> 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



More information about the Es4-discuss mailing list