Better support trailing quotes in single line raw strings #7723
-
Raw string literals make it very easy to create strings from text content. However, there is one case that is tricky and requires careful formatting of the code in order to get correct, while remaining a raw string. That is the case where you have a single-line raw string with a trailing double quote: var text = """date "2023-11-26""""; This is a compiler error as the compiler interprets the first three double quotes as the terminator of the string, and the following double quote is unexpected character. To use a raw string in this case you need to define it over multiple lines: var text = """
date "2023-11-26"
"""; This fixes the terminator problem, but adds another problem in that you need to be careful about the whitespace around the literal otherwise they may be interpreted as a part of the string. I would like to propose to amend single-line raw string literals so that if the string terminator is followed by more double quotes that the preceding double quotes are considered to be a part of the string, not the terminator. That way the single-line raw string literal above will compile successfully because the additional double quote character is reinterpreted to be a part of the terminator, and the leading double quote considered part of the string. There would be no limit to the number of double quotes to be included within the string: Console.WriteLine(""""This is a test!""""""); // prints This is a test!"" Advantages
Drawbacks
Workarounds
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Is this an actual spec change or an implementation bug? |
Beta Was this translation helpful? Give feedback.
-
The more I think about it the more I think I agree with the LDMs decision to forbid this. Asymmetry on the end of the string might be less problematic, but at the beginning of the string it would be very problematic and ambiguous as the compiler can't know whether the developer intended to start the string with additional quotes or use a longer terminator. I don't think it would be particularly helpful to solve for the end of the string if the beginning of the string still has the issue. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
The more I think about it the more I think I agree with the LDMs decision to forbid this. Asymmetry on the end of the string might be less problematic, but at the beginning of the string it would be very problematic and ambiguous as the compiler can't know whether the developer intended to start the string with additional quotes or use a longer terminator. I don't think it would be particularly helpful to solve for the end of the string if the beginning of the string still has the issue.