Skip to content

Make newlines sane again #7089

@arnetheduck

Description

@arnetheduck

As https://nim-lang.org/docs/manual.html#lexical-analysis-string-literals points out, '\n' can be used to get a newline in a string literal.

It doesn't say what a newline is however, except later where it's pointed out that it might not be a single character.

Per the compiler source, it's platform specific, and in practice it will mostly be translated to \x0a or \x0d\x0a in the C code.

Coming from other languages, this is a unwelcome surprise, where in practice, the \n sequence has a well established meaning of a line feed character (py, java, others) or "whatever-c-does" which at least guarantees a single character, and in practice ends up being a line feed (see https://en.wikipedia.org/wiki/Newline).

I'd like to suggest nim goes the way of py and friends, and defines the escape characters in terms of the ascii codes they generate - https://docs.python.org/2.0/ref/strings.html:

\a | ASCII Bell (BEL)
\b | ASCII Backspace (BS)
\f | ASCII Formfeed (FF)
\n | ASCII Linefeed (LF)
\r | ASCII Carriage Return (CR)
\t | ASCII Horizontal Tab (TAB)
\v | ASCII Vertical Tab (VT)

This is well-defined, easy to understand and follows the principle of least surprise. It is also the closest to what Nim is doing today, with exact codes rather than C-style free-for-all.

A platform specific newline can sometimes be useful - this can instead be mapped to a new escape character, for example \N to signal to the reader that there's unusual stuff going on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions