Skip to content

support escaping space in string literals using \s #20110

@unkarjedy

Description

@unkarjedy

Java multiline blocks supports \s escape sequence.

https://docs.oracle.com/en/java/javase/21/text-blocks/index.html

The \s escape sequence simple translates to space (\040, ASCII character 32, white space.) Since escape sequences don't get translated until after incident space stripping, \s can act as fence to prevent the stripping of trailing white space. Using \s at the end of each line in the following example, guarantees that each line is exactly six characters long.

String colors = """  red \s  green\s  blue \s  """;

Scala doesn't support \s escape sequence.
Scala 2 compiler crashes
image

Scala 3 compiler says

invalid escape '\s' not one of [\b, \t, \n, \f, \r, \, ", ', \uxxxx] at index 4 in "aaa \s bbb". Use \ for literal .

Given that \s is not reserved, the compiler could support by simply replacing with \s
At first glance, this should be trivial to fix.
This wouldn't work in non-interpolated multiline string literals and in raw string literals because they don't support escape sequences.
Still, it could still be a nice addition.
image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions