SIP-XX: Dedented Multiline String Literals

I agree it’s not great to have yet another string literal syntax. One mitigating factor is that ''' for triple-quoted strings are common (Python, Ruby, Dart, Elixir), so we’re not doing something unprecedented here. Many languages have multiple string syntaxes, and I expect that once ''' is rolled out there should be ~zero reason for anyone to use """.stripMargin strings ever again and they could be deprecated. So Scala would have 2 “current” syntaxes for strings and 1 deprecated one left in purely for backwards compat

I would personally be ok with using a single-double-quote for multiline strings, though we need to think carefully about the tradeoffs. Apart from the subjective style, multiline single quotes " have one major disadvantage in that they won’t support extended delimiters

We allow Extended Delimiters with more than three ''', to allow the strings to contain arbitrary
contents, similar to what is provided in C# and Swift. e.g. if you want the string to > > contain ''', you can use a four-'''' delimiter to
stop the ''' within the body from prematurely closing the literal:

> def helper = {
    val x = ''''
    '''
    i am cow
    hear me moo
    '''
    ''''
    x
  }

println(helper)
'''
i am cow
hear me moo
'''

Extended delimiters (present in C#, Swift, and similar to Ruby HEREDOC) allow ANY literal string to be embedded in source code by choosing an appropriate delimiter before and after. This basically turbo-charges the “Raw” string nature of our existing triple-quoted strings, which are great but have problems embedding """s within themselves.

Requiring the closing " to be alone of the line doesn’t work: apart from it being convenient to put ,s and other things after the closing delimiter, it also would prohibit us from defining strings containing a single double quote on its own line:

val s = "
  "
"

@sjrd’s solution of requiring one more layer of indentation within the text block has a similar problem: how would that work in the case above, where we want the multi-line string to contain a single quote?

It seems that without some sort of extended delimiter, we’d have no choice but to have some multiline strings need escaping for certain characters. Not the end of the world, but it does partially defeat the purpose of multiline strings which is to avoid syntactic noise

1 Like