Escaping newlines in multi-line string macros?

Would this feature, to be able to write a single string literal on multiple lines of the source code without \n in the string,

be eventually implemented for all string-like objects?

I was surprised that it doesn’t work for StrFormat:

using StrFormat

# This string is equivalent to "abc".
s = "a\
 b\
    c"

# This string is an error.
t = f"a\
 b\
    c"

Perhaps one needs to submit a feature request to each package that offers string-like literals?

(By the way, how this work? Does this mean that StrFormat does not use the parser of String?)

StrFormat’s literal f"..." syntax is an example of a “string macro” or non-standard string literal.

The original PR julia#40753 about escaping newlines explicitly says that is only for “non-custom strings”.

The basic reason for this, I think, is that custom strings foo"..." are parsed very differently than standard strings "..." in that Julia does (almost) no escaping for custom strings — they are passed literally as-is to the macro (which can then implement whatever escaping it wants). Backslashes are not treated as escapes (except before the trailing ", see below).

This is very important for e.g. regex parsing, in that if you do r"\s+" you want the \s to be passed literally to the regex engine. If you used a standard string, e.g. with the Regex constructor, you would have to escape the backslash: Regex("\\s+"). This quickly becomes unreadable for complicated regexes. (Another example is that people often express Windows paths as raw"..." strings, which are simply an identity-function string macro, in order to avoid escaping all the backslashes.)

So, I guess the thought is that treating a \ followed by a newline in a string macro as an escape would be odd in that context, since \ inside a string macro is otherwise mostly treated as a literal backslash (except before the trailing ", which uses a mind-numbing escaping rule). Though it wouldn’t be impossible AFAICT.

Note that you can always use triple-quoted strings """ ... """ in order to have a multi-line string literal. This works with string macros too.

(So I don’t quite see what purpose was served by implementing backslash-escaped newlines for single-quoted strings in the first place. It isn’t explained in the PR.)

I apologize beforehand if I’m mistaken but it seems to me that you mistake the issue. You mention “backslash-escaped newlines” but that’s not the subject of the present discussion. With the triple quoted string, you get newlines embedded in the string:

s = """a
 b
    c"""
# ->  "a\nb\n   c"

whereas the feature we are talking about is to prevent newlines from entering the String object.

The original issue was/is that without this feature, you would write something like this:

s = "$(somevar) blah blah" *
   "blah blah $(othervar) blah blah" *
   "blah blah $(something) blah blah"

to get a string object that doesn’t include newline characters.