TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

C# Raw String Literal Proposal

89 pointsby nikbackmover 3 years ago

27 comments

chubotover 3 years ago
<i>To make the text easy to read and allow for indentation that developers like in code, these string literals will naturally remove the indentation specified on the last line when producing the final literal value.</i><p>This is the same rule that Oil has; I think it came from the Julia language (or at least that&#x27;s where I got it from)<p><i>Oil Has Multi-line Commands and String Literals</i> <a href="http:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2021&#x2F;09&#x2F;multiline.html" rel="nofollow">http:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2021&#x2F;09&#x2F;multiline.html</a>
__ryan__over 3 years ago
I&#x27;ve given some thought to this kind of string literal in the past (for my imaginary programming language). I want a syntax <i>something</i> like this:<p><pre><code> var xml = &quot;&quot;&quot;&lt;element attr=&quot;content&quot;&gt; &quot;&quot;&quot; &lt;body&gt; &quot;&quot;&quot; &lt;&#x2F;body&gt; &quot;&quot;&quot;&lt;&#x2F;element&gt; &quot;&quot;&quot;; </code></pre> This would give you the string:<p><pre><code> &lt;element attr=&quot;content&quot;&gt; &lt;body&gt; &lt;&#x2F;body&gt; &lt;&#x2F;element&gt;(no newline) </code></pre> If you wanted a newline at the end, you&#x27;d do this:<p><pre><code> var xml = &quot;&quot;&quot;&lt;element attr=&quot;content&quot;&gt; &quot;&quot;&quot; &lt;body&gt; &quot;&quot;&quot; &lt;&#x2F;body&gt; &quot;&quot;&quot;&lt;&#x2F;element&gt; &quot;&quot;&quot; &quot;&quot;&quot;; </code></pre> Basically the end delimiter of the string would be the last &quot;&quot;&quot;. You could concatenate two strings like so:<p><pre><code> var xml = &quot;&quot;&quot;&lt;element attr=&quot;content&quot;&gt; &quot;&quot;&quot; &lt;body&gt; &quot;&quot;&quot; &lt;&#x2F;body&gt; &quot;&quot;&quot;&lt;&#x2F;element&gt; &quot;&quot;&quot; &quot;&quot;&quot; &#x2F;&#x2F; this string ended on this line + &quot;&quot;&quot;&lt;element attr=&quot;content&quot;&gt; &quot;&quot;&quot; &lt;body&gt; &quot;&quot;&quot; &lt;&#x2F;body&gt; &quot;&quot;&quot;&lt;&#x2F;element&gt; &quot;&quot;&quot; &quot;&quot;&quot;; &#x2F;&#x2F; this string ended on this line </code></pre> This could use the same logic for using <i>at least</i> three quotes as the indicator that it&#x27;s a multiline string.<p>Please, tear this apart and offer improvements.<p>Edit: this is conceptually similar to Zig&#x27;s multiline literal: <a href="https:&#x2F;&#x2F;ziglang.org&#x2F;documentation&#x2F;master&#x2F;#Multiline-String-Literals" rel="nofollow">https:&#x2F;&#x2F;ziglang.org&#x2F;documentation&#x2F;master&#x2F;#Multiline-String-L...</a>
评论 #30373498 未加载
评论 #30381658 未加载
评论 #30373799 未加载
评论 #30385406 未加载
hankchinaskiover 3 years ago
I have been temporarily working with c# for the past month after years of Go. It’s a different philosophy to Go, there is a lot of syntactic sugar and magic spells that make your life easy… but I don’t know if I prefer that to the Go way of doing things. I was pleasantly surprised tho. Much better experience than working with Java
评论 #30373396 未加载
评论 #30381433 未加载
评论 #30373389 未加载
crispyambulanceover 3 years ago
I got confused at the first example.<p><pre><code> var xml = &quot;&quot;&quot; &lt;element attr=&quot;content&quot;&gt; &lt;body&gt; &lt;&#x2F;body&gt; &lt;&#x2F;element&gt; &quot;&quot;&quot;; </code></pre> And then they say that xml gets this...<p><pre><code> &lt;element attr=&quot;content&quot;&gt; &lt;body&gt; &lt;&#x2F;body&gt; &lt;&#x2F;element&gt; </code></pre> But they <i>don&#x27;t</i> explicitly say if the new lines after and before the &quot;&quot;&quot; &#x27;s are considered part of the literal string or not.<p>Are they?
评论 #30376804 未加载
评论 #30373405 未加载
评论 #30373366 未加载
评论 #30373387 未加载
wvenableover 3 years ago
I&#x27;ve used this in other languages and I don&#x27;t like it. It&#x27;s the wrong approach to solving the problem. As soon as I want to embed some multi-line string in my code then I&#x27;d be better of just having it in it&#x27;s own file.<p>.NET does allow you embed files directly into your project and read in those files but it&#x27;s a lot of confusing boilerplate.<p>If .NET provided a really easy way to take a file from the project and create a compile-time string from it then I think that would be significantly more useful than this proposal.
评论 #30386119 未加载
评论 #30387074 未加载
ape4over 3 years ago
Perl&#x27;s heredoc is better. You get to pick the delimiter. <a href="https:&#x2F;&#x2F;perlmaven.com&#x2F;here-documents" rel="nofollow">https:&#x2F;&#x2F;perlmaven.com&#x2F;here-documents</a>
gwbas1cover 3 years ago
... Why?<p>There are so many different ways to do strings in C#. Adding features like this just makes the language harder to learn, and the compiler harder to implement.<p>At this point, it&#x27;s probably better to adjust the compiler to make it easier to turn a text file into a hardcoded string. The embedded resource approach works, but it could be significantly smoother.<p>Or, maybe the compiler needs some form of a plugin architecture so people who want obscure features can figure out how to add them?
评论 #30376963 未加载
评论 #30373501 未加载
评论 #30373438 未加载
LandRover 3 years ago
<p><pre><code> var v = &quot;&quot;&quot;&quot;&quot;&quot; contents&quot;&quot;&quot;&quot;&quot; &quot;&quot;&quot;&quot;&quot;&quot; </code></pre> lol.<p>I like the proposal to have these sort of raw strings, where indentations are removed, but can&#x27;t they use a symbol before the string like they do with interpolation `$` or literals `@`?<p>I know it says design decision to go with 1 more &quot; than the longest sequence of &quot; in the string, but why ?
评论 #30373486 未加载
评论 #30376881 未加载
评论 #30374149 未加载
评论 #30373166 未加载
评论 #30372995 未加载
评论 #30373820 未加载
captainmuonover 3 years ago
Nice, I&#x27;m surprized this is not already in the language. The only thing I find a bit strange is that the delimiters must be on separate lines (unless it is the special one-line-form). So this is apparantly not legal:<p><pre><code> var s = &quot;&quot;&quot;This is a multiline string&quot;&quot;&quot;; </code></pre> Requiring the start and especially end quotes to be on a separate line makes it take a lot of vertical space. But OTOH, that is consistent with the default coding style in C# which is vertically verbose (with {} on lines by themselves).
评论 #30373415 未加载
评论 #30376818 未加载
billpgover 3 years ago
A little while ago, I discovered that...<p><pre><code> $&quot;A{new List&lt;string&gt;{$&quot;B{&quot;{C}&quot;}D&quot;}.First()}E&quot; </code></pre> ... was valid C#.<p>This means that a C# compiler can&#x27;t start with a simple tokenizing loop. That compiler phase would have to keep track of state in a stack, recording what each } character means while its still looping through code character-by-character.<p>Now we&#x27;re adding {{ and }} into the equation. Yay.
评论 #30374544 未加载
评论 #30376929 未加载
评论 #30385954 未加载
exyiover 3 years ago
I hope it will also normalize newlines to `\n`. The current version of raw literals (@&quot;...&quot;) just puts there whatever is in the file, so it in practice depends on if your program was compiled on Windows or Linux. Surely that should be irrelevant for the compilation to intermediate language
评论 #30377055 未加载
评论 #30373325 未加载
iliketrainsover 3 years ago
This is fantastic! After this is in, can anyone propose a better documentation syntax please? I am really tired of typing verbose XML as comments, and not even being able to write &quot;a &lt; b&quot; or &quot;T&lt;int&gt;&quot; in my comments...
DalekBaldwinover 3 years ago
Interpolation is its own can of worms, but if you just want to be able to encode absolutely anything without escape characters, you just need two delimiters:<p><pre><code> &quot;*n [stringA] &quot;*n &quot;*n [stringB] &#x27;*n &#x27;*n [stringC] &quot;*n &#x27;*n [stringD] &#x27;*n </code></pre> Each string may contain runs of contiguous single or double quotes of length less than n, and furthermore:<p>stringA may start and&#x2F;or end with a single quote<p>stringB may start with a single quote and&#x2F;or end with a double quote<p>stringC may start with a double quote and&#x2F;or end with a single quote<p>stringD may start and&#x2F;or end with a double quote
jsd1982over 3 years ago
For the single-line case, what happens for:<p><pre><code> &quot;&quot;&quot;&quot;&quot;&quot;&quot; &quot;&quot;&quot;&quot;&quot;&quot;&quot;&quot; </code></pre> Are those strings containing `&quot;` and `&quot;&quot;` or are they empty strings? Is the first case an error because the starting and ending quote counts do not match? If the number of quote chars is even, do the contents alternate between `&quot;` and empty as the number of surrounding quotes increases?
评论 #30373620 未加载
chhickmanover 3 years ago
I would much rather see something like this:<p><pre><code> string longString = `This ` allows ` differentiation ` of `formatting ` indention ` from ` leading ` string ` spaces ` using ` back-ticks (\`)</code></pre>
评论 #30377076 未加载
a9h74jover 3 years ago
In place of all of the special rules for handling indentation, I wonder if they could simply define some extra starting chars (besides $$&quot;&quot;&quot; for controlling interpolation) to indicate suppress-leading-newline or suppress-ending-newline etc. Offhand this would seem more explicit than implicit, and be searchable (unlike a pattern).<p>Other than that, ++ for any mechanism to quote to arbitrary depth. I have imagined<p>[abcfoo[ ...anything but ]abcfoo]... ]abcfoo]<p>as another approach.
StevePerkinsover 3 years ago
I like the three-quote literal syntax in general, and am a bit surprised that C# doesn&#x27;t have this already. Even Java has had this for awhile now!<p>But I don&#x27;t like the indented form, where nested triple-quotes are ignored. Whitespace formatting is fine when I&#x27;m working with Python, but I really don&#x27;t want to mix that paradigm when I&#x27;m working with curly-brace languages.
评论 #30373503 未加载
ducharmdevover 3 years ago
&gt; This allows code to look natural, while still producing literals that are desired, and avoiding runtime costs if this required the use of specialized string manipulation routines.<p>Maybe a source generator would be applicable here? Not as nice has having it built into the language of course, but it would at least eliminate these runtime costs.
KingOfCodersover 3 years ago
With all the hacks and exploits going on, and (Log4J) more security awareness coming,<p>I&#x27;ve been considering a safe String class that prevents some characters like CR,LF,\ that are seldom needed in business strings but used in system level things. Drawing a line between these two would increase security.
评论 #30375375 未加载
评论 #30373522 未加载
assbuttbuttassover 3 years ago
I always found these complex indentation-stripping rules to be confusing in a language such as Python. I was under the impression that C# doesn&#x27;t treat whitespace significantly, so why do they need all these complex rules? Just interpret what&#x27;s between the quotes literally.
评论 #30373087 未加载
评论 #30373085 未加载
torginusover 3 years ago
It seems like .NET is trying to outcompete Rust in the number of string types available in the language.
评论 #30373291 未加载
评论 #30373334 未加载
评论 #30373298 未加载
monadmoproblemsover 3 years ago
I&#x27;ve long wanted a more succinct way of writing implicitly typed arrays. Whenever you work with data directly in the code, for example when hacking on leetcode, you end you with lots of horrible nested arrays:<p>new [] {new [] {1, 2}, new [] {3, 4}};<p>Something like:<p>@[ @[1, 2], @[3, 4] ]
评论 #30377094 未加载
radicalbyteover 3 years ago
The issue is being discussed here: <a href="https:&#x2F;&#x2F;github.com&#x2F;dotnet&#x2F;csharplang&#x2F;issues&#x2F;4304" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dotnet&#x2F;csharplang&#x2F;issues&#x2F;4304</a>
jmullover 3 years ago
I guess a good thing about proposals like this is as an early warning system.<p>When a language gets its fourth or fifth string literal syntax, its process is probably broken.
jimwormover 3 years ago
Heredoc by another name...?
评论 #30373265 未加载
intrasightover 3 years ago
I did not understand the xml indentation examples
评论 #30377108 未加载
jwsteigerwaltover 3 years ago
Yes!!!!