# SIP-XX: Dedented Multiline String Literals

**URL:** <https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197>\
**Category:** Language Design\
**Created:** [August 15, 2025, 7:07pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197 "2025-08-15T19:07:26Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [August 17, 2025, 2:40pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/42 "2025-08-17T14:40:34Z")

</div>

…or Github issues/PR descriptions, readme.md, and other places. This is mentioned in the proposal as a downside and I think it’s probably a blocker

---

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [August 17, 2025, 3:11pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/43 "2025-08-17T15:11:07Z")

</div>

One example where the “terminate parsing only when there’s a single `"` on that line” would cause problems is the following:

```scala
val lines = "
  this is the
  expected result
".split("\n")

```

This is a pretty common pattern, and I’ve written similar code many times. In this case, the `"` would not be recognized as a multiline string terminator because of the `"`s in the following `"\n"`

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [August 17, 2025, 3:21pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/44 "2025-08-17T15:21:46Z")

</div>

True. You’d have to write

```scala
val lines = "
  this is the
  expected result
  "
  .split("\n")

```

If we want to allow

```scala
val lines = "
  this is the
  expected result
  ".split("\n")

```

we could generalize the rule further and say a `"` terminates a multi-line string if

- it is only preceded on the same line by whitespace, and
- it is followed by an even number of `"` on the same line (including 0)

That would leave a program with two multi-line strings separated by `,` or whatever as the only false negative.

Instead of the spec langsuage above one could then explain it like this: _you can’t have a multi-line string literal start at the same line where another one ends_. That’s easy to remember.

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [August 17, 2025, 3:30pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/45 "2025-08-17T15:30:33Z")

</div>

> [@odersky](#):
>
> That would leave a program with two multi-line strings separated by `,` or whatever as the only false negative.

So the following is considered to be a single parameter application

```auto
foo(
  "
  this is
  ","
  not a drill
  "
)

```

And this two parameter application

```auto
foo(
  "
  this is
  ",
  "
  not a drill
  "
)

```

Did I understand you correctly?

---

<div class="post-metadata">

**Author:** ![odersky](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/odersky/32/42_2.png) [@odersky](https://contributors.scala-lang.org/u/odersky)\
**Post date:** [August 17, 2025, 3:37pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/46 "2025-08-17T15:37:07Z")

</div>

Yes, that’s what I meant.

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [August 17, 2025, 3:45pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/47 "2025-08-17T15:45:22Z")

</div>

I thought of another limitation of a single `"` is that it makes any string you start typing greedy for the parser, so everything is starting to be colored like string until somewhere down the code it hits a new `"`.

For this reason I think we mandate a multi-line single `"` to be followed by white-spaces only until a new line. This means that a start of multi-line string is `"` + newline (with optional whitespaces between them).  
The first line drop will _not_ be considered for a new line a part of the string.  
Examples:

```auto
print("hello\n")
print("
world
")
print("goodbye")

```

Printout:

```auto
hello
world

goodbye

```

This will create a parser error like Scala today.

```auto
print("my
attempt
") 

```

---

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [August 17, 2025, 3:50pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/48 "2025-08-17T15:50:36Z")

</div>

> [@soronpo](#):
>
> The first line drop will _not_ be considered for a new line a part of the string.  
> Examples:
> 
> ```auto
> print("hello\n")
> print("
> world
> ")
> print("goodbye")
> 
> ```
> 
> Printout:
> 
> ```auto
> hello
> world
> 
> goodbye
> 
> ```
> 
> This will create a parser error like Scala today.
> 
> ```auto
> print("my
> attempt
> ") 
> 
> ```

In the proposal, neither the leading nor trailing newline are included in the string. This is in line with implementations in other languages (C#, Swift, Ruby), so your example would print

```auto
hello
world
goodbye

```

Also, the existing proposal already mandates that the opening delimiter is followed by a newline and nothing else

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [August 17, 2025, 3:51pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/49 "2025-08-17T15:51:53Z")

</div>

> [@lihaoyi](#):
>
> Also, the existing proposal already mandates that the opening delimiter is followed by a newline and nothing else

The existing proposal for a single `"`? Sorry, I missed that.

---

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [August 17, 2025, 3:52pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/50 "2025-08-17T15:52:20Z")

</div>

For the `'''` syntax, which I suppose forms the basis for the alternate multiline `"` syntax

---

<div class="post-metadata">

**Author:** ![Ichoran](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ichoran/32/40_2.png) [@Ichoran](https://contributors.scala-lang.org/u/Ichoran)\
**Post date:** [August 17, 2025, 6:02pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/51 "2025-08-17T18:02:41Z")

</div>

Why not simply insist that the indented text be _deeper_?

If you have a trailing `"` on a line, this opens a multi-line string, stripped to the level of indentation of the first (nonempty) line.

The string terminates at the first instance of `"` starting at the indentation depth of the line that opened the multi-line string.

This allows all manner of splits, closing and reopening, and so on, in a way that is irregular but still visually very clear:

```scala
val addresses = Array(
  "
    123 Main Street
    Boise, Idaho
  ", "(none)", "
    P.O. Box 432
    Tinyville, MiddleOf, Nowhere
  "
)

```

Awkward, not the best form, but also still pretty clear what it is supposed to mean (_edit: at least now that I’ve fixed my indentation typos…_).

The only thing this would _prevent_ is quoting indented paragraphs:

```auto
val quote = "
    As she pushed off, she knew that
  no one would see this sandbar again.
"

```

If we really wanted to allow that, we could _also_ have the bare-quote-with-nothing-else rule with flat (same-level indent) parsing:

```scala
val quote =
  "
    As she pushed off, she knew that
  no one would see this sandbar again.
  "

```

But I think the cleanest and most intuitive basic rule is: open with single quote, close with single quote _at the indentation level of the line it started on_, the text inside has the indentation removed, and you stop when you hit the quote at the correct indentation level (and it’s an error if it’s indented less).

If you want a fancier parser, you can capture the whole set of lines and dedent the _minimum_ indent level of the block; that would allow the quote above too. But if you want to go line-by-line, I think the no paragraph indents rule is fine.

The only thing that is missing then is

```scala
val indented =
  "
    I really do mean
    to print this out
    indented two spaces
  "

```

But given that this is easily solved by an `.indent` method, I don’t think it’s a major barrier.

(Note: Java has `indent` since 12; if we need to support 11 or less, we could add an `indentWith(s: String)` method that would both allow more flexibility and avoid the name collision with later Java.)

---

<div class="post-metadata">

**Author:** ![satorg](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/satorg/32/3430_2.png) [@satorg](https://contributors.scala-lang.org/u/satorg)\
**Post date:** [August 17, 2025, 6:23pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/52 "2025-08-17T18:23:53Z")

</div>

> [@lihaoyi](#):
>
> Also, the existing proposal already mandates that the opening delimiter is followed by a newline and nothing else

How about using this fact for the benefit of better clarity and disambiguation?

When a multiline string opens with `"`, then it _can_ allow non-whilte characters XYZ until EOL. Then, XYZ should occur right _before_ the closing `"` in order to close the string. Any occurence of a single `"` without preceding XYZ is not considered as a closing one and therefore doesn’t require escaping.

For example:

```plaintext
val json =
  "---
  {
    "one": 1,
    "ruler": " --- " // it's ok since `---` not followed by `"`
  }
  ---".toJson

def openingParagraph = "#-#
  one dark and stormy 
  night, he said
  "...i am cow 
  hear me moo"
  #-#"

// Any sequence of chars after opening " can be used
// therefore `[expected]` will be effectively removed from the string.
assertEquals(
  "[expected]
  this is the
  expected result
  [expected]",
  computation()
)

```

A caveat: the XYZ sequence cannot contain `"` itself because it will make it a single-line string.

Would it be too crazy to implement?

---

<div class="post-metadata">

**Author:** ![lihaoyi](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/lihaoyi/32/103_2.png) [@lihaoyi](https://contributors.scala-lang.org/u/lihaoyi)\
**Post date:** [August 17, 2025, 6:26pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/53 "2025-08-17T18:26:37Z")

</div>

> [@satorg](#):
>
> Would it be too crazy to implement?

I think it could work! It would be similar to the HEREDOC from Ruby. With proper choice of header, such as the `"--- ---"` you demonstrated, it looks great too. And it entirely removes the need for escaping, since the header/footer strings are customizable. And it doesn’t require a new `'''` quote style to be introduced.

---

<div class="post-metadata">

**Author:** ![devlaam](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/devlaam/32/2395_2.png) [@devlaam](https://contributors.scala-lang.org/u/devlaam)\
**Post date:** [August 17, 2025, 6:43pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/54 "2025-08-17T18:43:24Z")

</div>

> [@satorg](#):
>
> A caveat: the XYZ sequence cannot contain `"` itself because it will make it a single-line string

Unless XYZ equals the double quote `""`, in which case it interpreted as the regular three quoted multiline string definition 😉

I think this proposal is nice, since there is always some combi XYZ that is not part of the string to be defined.

> [@satorg](#):
>
> Would it be too crazy to implement?

If so, we could still opt for [my third option](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/19): always close a multiline string started with a single quote with a triple quote (with internal triple quote’s to be escaped).

---

<div class="post-metadata">

**Author:** ![soronpo](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/soronpo/32/533_2.png) [@soronpo](https://contributors.scala-lang.org/u/soronpo)\
**Post date:** [August 17, 2025, 7:48pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/55 "2025-08-17T19:48:50Z")

</div>

> [@satorg](#):
>
> When a multiline string opens with `"`, then it _can_ allow non-whilte characters XYZ until EOL. Then, XYZ should occur right _before_ the closing `"` in order to close the string. Any occurence of a single `"` without preceding XYZ is not considered as a closing one and therefore doesn’t require escaping.

It’s nice, but doesn’t it make things worse by creating new flexibility of headers? Why not just choose `"-- --"`? And if we do this, how is it really different than the original proposal?

---

<div class="post-metadata">

**Author:** ![satorg](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/satorg/32/3430_2.png) [@satorg](https://contributors.scala-lang.org/u/satorg)\
**Post date:** [August 17, 2025, 8:08pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/56 "2025-08-17T20:08:22Z")

</div>

I guess, it could become just one string literal `" ... "` that covers both single-line strings and multiline strings. And it would allow to customize the way how each string should be closed based on its content, which is only known to a user at the moment. Therefore, if chosen properly, less-to-no escaping would be necessary.

---

<div class="post-metadata">

**Author:** ![satorg](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/satorg/32/3430_2.png) [@satorg](https://contributors.scala-lang.org/u/satorg)\
**Post date:** [August 17, 2025, 8:50pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/57 "2025-08-17T20:50:46Z")

</div>

Just to clarify: additional XYZ are not required for multiline strings:

```plaintext
// https://en.wikipedia.org/wiki/What_a_piece_of_work_is_a_man
val simpleQuote = "
  What a piece of worke is a man! how Noble in
  Reason? how infinite in faculty, in forme, and mouing
  how expresse and admirable in Action, how like an Angel
  in apprehension, how like a God?
  " 

```

However, they can be added depending on the string content, when necessary:

```plaintext
val scalaMultilineStringsDoc =
  "~~~
  Scala allows to define multiline strings. Example:
  
  val jsonText = "---
    {
      "one": 1,
      "two": 2,
      "three": 3
    }
    ---"

  Any character sequence can be used after the opening `"`.
  This sequence will be used to identify the closing `"` and differentiate it from any other `"` thay may occur withing the string.
  ~~~"

```

---

<div class="post-metadata">

**Author:** ![satorg](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/satorg/32/3430_2.png) [@satorg](https://contributors.scala-lang.org/u/satorg)\
**Post date:** [August 17, 2025, 9:05pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/58 "2025-08-17T21:05:48Z")

</div>

> [@devlaam](#):
>
> Unless XYZ equals the double quote `""`, in which case it interpreted as the regular three quoted multiline string definition 😉

Yes, this would be an unfortunate but necessary exception. I guess, if some better multiline string approach would have been implemented, then the triple-quoted strings could be deprecated and then decomissioned over time.

---

<div class="post-metadata">

**Author:** ![Ichoran](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ichoran/32/40_2.png) [@Ichoran](https://contributors.scala-lang.org/u/Ichoran)\
**Post date:** [August 17, 2025, 11:43pm UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/59 "2025-08-17T23:43:10Z")

</div>

I really don’t think this is necessary with indentation-aware parsing.

```scala
val scalaMultilineStringsDoc = "
  Scala allows defining of multiline strings. Example:
  
  val jsonText = "
    {
      "one": 1,
      "two": 2,
      "three": 3
    }
  "

  A multiline string begins with a trailing, non-matching quote
  at the end of a line. Subsequent lines must be indented
  more deeply than the line on which the trailing quote started.
  The multiline string is ended by a single quote at the indentation
  depth of the line on which the trailing quote was given.

  The indentation level of the first nonempty line after the quote is
  removed from every line. Thus, the example above is equivalent to

  val jsonText =
    """{""" + "\n" +
    """ "one": 1,""" + "\n" +
    """ "two": 2,""" + "\n" +
    """ "three": 3,""" + "\n" +
    """}""" + "\n"
 "

```

If we really really want to be able to specify the indentation depth, we could use `<-` on the start line to mark where the boundary is with the start-quote on its own line

```scala
val indented =
  " <-
      This is indented two spaces.
    This is flush with the left.
  "

val disallowed =
  " <-
    This is indented -2 spaces! Compiler error!
        This is okay (it starts with 2 spaces)
  "

```

Because `<-` is a reserved symbol, there is very little risk of it accidentally appearing there as an unquoted string. And if you want to include a literal `<-`, you just do it like normal in the block of text.

This is cleaner than delimiter marks. We already have spaces to act as delimiter marks! The problem with delimiters is that they require you to be aware of the content–if the content changes (e.g. you’re quoting code), then you can mess up your string. Indentation, in contrast, only requires you to indent properly, which you already committed to doing by using an indentation-aware format. “Copy, and hit tab until it’s deeper” is pretty straightforward.

---

<div class="post-metadata">

**Author:** ![Ichoran](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/ichoran/32/40_2.png) [@Ichoran](https://contributors.scala-lang.org/u/Ichoran)\
**Post date:** [August 18, 2025, 12:03am UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/60 "2025-08-18T00:03:02Z")

</div>

If we are going to do this, we should pick a small number of characters that are okay, and probably limit it to repeats of the same character so we don’t have ASCII-art header/trailers. `#` is common, but I agree that `-` and `~` look cool too. I don’t think `#-#` is a good idea. Anything that looks too fancy suggests that it has meaning, which it doesn’t.

---

<div class="post-metadata">

**Author:** ![mberndt](https://contributors.scala-lang.org/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@mberndt](https://contributors.scala-lang.org/u/mberndt)\
**Post date:** [August 18, 2025, 12:12am UTC](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197/61 "2025-08-18T00:12:20Z")

</div>

> [@satorg](#):
>
> When a multiline string opens with `"`, then it _can_ allow non-whilte characters XYZ until EOL. Then, XYZ should occur right _before_ the closing `"` in order to close the string. Any occurence of a single `"` without preceding XYZ is not considered as a closing one and therefore doesn’t require escaping.

This is a bit remeniscent of Perl’s string quoting operators. These start with `q` and are followed by a special character which is the string delimiter. `q"moo"`, `q'moo'`, `q#moo#` etc. all mean the same thing, e. g. `q(foo)` or `q{foo}`. There’s an additional twist for parenthesis-like characters: a string literal started with an opening paren is closed with the corresponding closing paren. I rather like that too.

[Previous page](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197.md?page=2)

[Next page](https://contributors.scala-lang.org/t/sip-xx-dedented-multiline-string-literals/7197.md?page=4)
