[Proposal]: Raw string literal #8646
Replies: 329 comments
-
is there a way that literal be combined with $ to embed tokens, or is this completely literal strings only?
(expecting that the answer is 'no - raw means raw with no interpolation' ) |
Beta Was this translation helpful? Give feedback.
-
I personally think our existing strings are good enough for the template literal case. This is only for raw strings, and is intended when you just want to take a real snippet of some other language out there and put it into C#. The moment we allow things like So, tbh, i believe this should just be for raw-strings. And the best way to handle that is to make sure that you can provide a literal that will never conflict with the contents, and that the contents can't ever have meaning. :) |
Beta Was this translation helpful? Give feedback.
-
In my opinion, Example 2 should trow an error and is confusing. |
Beta Was this translation helpful? Give feedback.
-
"Perhaps this makes it also easier for the parser." |
Beta Was this translation helpful? Give feedback.
-
Thats easy. Add an empty line No empty line at the end. Last character in the string is >
with empty line at the end
For me, that is much more intuitive and logical |
Beta Was this translation helpful? Give feedback.
-
Actually, I believe the escalating problem is about double-quote marks specifically. Having a raw-string+interpolation should therefore be possible and useful for at least HTML,XML and non-C markup/languages, but this is something that can be deferred for the future. Examples:
(I am using a single-line mode for these examples for brevity) Examples with raw strings that would have braces:
Although the braces still need escaping, the ability to include raw double-quotes makes this much easier to read. |
Beta Was this translation helpful? Give feedback.
-
I don't see any conceptual paradox in having single-line and multi-line raw string literals with de-indentation.
Perhaps it isn't impossible to implement, but it would be much more complex or the spec-system isn't flexible enough? |
Beta Was this translation helpful? Give feedback.
-
I disagree. There's nothing confusing about the closing quote being on the same line. It's exactly how text blocks work in Python and Java and it is not a problem in either of those languages. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
Other languages are welcome to do what they wish, but given two major languages have adopted the behavior proposed above it demonstrates that there is nothing inherently confusing about it. |
Beta Was this translation helpful? Give feedback.
-
Because major languages have some features doesn't automatically mean that it isn't confusing. |
Beta Was this translation helpful? Give feedback.
-
Many different ways to skin that cat. To be honest I kind of prefer C++'s general approach to raw strings over text blocks since you're given a lot of flexibility to customize the delimiters while still retaining the syntax of a string (unlike heredocs in many languages). See the syntax I originally proposed here: #89 I will admit that having the closing delimiter on a separate line does make it easier to control the indentation without including that final newline character, and Cyrus was a little surprised that Java does include that newline when the delimiter is on the next line (so does Python). |
Beta Was this translation helpful? Give feedback.
-
It does help with the argument though. Ultimately, either approach will need to be learned. Given that this doesn't really seem to have been a problem for many other languages, I'm not too worried for us. That said, I'm certain we'll discuss that option when we design this. |
Beta Was this translation helpful? Give feedback.
-
I'm certain we'll discuss this during the design process. |
Beta Was this translation helpful? Give feedback.
-
I'm certain we'll discuss this during the design process. |
Beta Was this translation helpful? Give feedback.
This comment has been minimized.
This comment has been minimized.
-
Because there would be no defined behavior and it would differ between each user environment. |
Beta Was this translation helpful? Give feedback.
-
Is there a way to make this work when compiling a framework application (i.e. not 11) but using 17.0+? This is an EXTREMELY useful feature, and it's a real shame if it can't be used with older projects since it is really only the compiler that needs to know about it. |
Beta Was this translation helpful? Give feedback.
-
@jrmoreno1 not sure wha you mean. There are no restrictions on using this feature. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi : I am using the latest version of VS (17.6.2), with a console application targeting net48 (which will automatically target language version 7.3), results in CS8730. But since this is entirely compiler magic and both my build server and any local builds will be with a compiler that supports this feature, it would be terrific if it worked. It's very annoying to start typing and then get an error message telling me that I have to do a lot of escaping to get things to work... |
Beta Was this translation helpful? Give feedback.
-
You can manually set the language version. This is a requirement. If you have your lang version set to something lower (implicitly or explicitly) it will of course be blocked as that's literally what a language version is (a claim about which versions of the language you desire to use). Use the |
Beta Was this translation helpful? Give feedback.
-
This is technically not a supported scenario, though, right? I thought language versions were now tied to runtime versions, regardless of whether or not runtime changes were required to support any specific features, in order to avoid confusion as to which combination of features would work. |
Beta Was this translation helpful? Give feedback.
-
it's not supported. but it's something that can be done. We're not going to stop you. We're not going to spend any effort on going and making these scenarios supported though. |
Beta Was this translation helpful? Give feedback.
-
Should this be closed now that the proposal has gone through? |
Beta Was this translation helpful? Give feedback.
-
@KyleMit We have not added this to the ecma standard yet. |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
@julealgon Feel free to open a discussion on this. This issue is not the location to meta discus process. |
Beta Was this translation helpful? Give feedback.
-
Raw string literal
Summary
Allow a new form of string literal that starts with a minimum of three
"""
characters (but no maximum), optionally followed by anew_line
, the content of the string, and then ends with the same number of quotes that the literal started with. For example:Spec: https://github.com/dotnet/csharplang/blob/main/proposals/raw-string-literal.md
Special thanks to @jnm2 for a deep review of this proposal
Beta Was this translation helpful? Give feedback.
All reactions