Why do regular interpolated strings not allow repeating the $ like raw string literals do? #6935
-
I was just preparing a few slides for my coworkers to highlight new features in C#11, and while writing up the section for raw string literals I wondered..."does this work on regular interpolations?" (Spoiler: the answer is no, and this discussion is to find out why) Raw string literals let us do this (despite having their main use-case as multi-line literals, which are otherwise a pain to type out/escape things): int theAnswer = 42;
string javaScriptCode = $$"""var theQuestion = { expectedAnswer: {{theAnswer}}, prompt: 'What is the answer to life, the universe and everything?' };"""; But regular interpolations do not allow this: int theAnswer = 42;
string javaScriptCode = $$"var theQuestion = { expectedAnswer: {{theAnswer}}, prompt: 'What is the answer to life, the universe and everything?' };"; Visual Studio 2022 17.4 correctly does the code-highlighting, but brings up The error is somewhat odd as well, because it feels like the Granted, this saves the whole lot of 4 characters (and could've been a lot uglier if I chose to name the variable So, to type it out loud: Is there any specific reason why this was disallowed? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There is no decision to disallow. The language works in the opposite direction. We decide what to allow. There was never any proposal or suggestion to allow this, so the concept has never even come up for discussion. :) If you'd like this to be allowed, we can work through a proposal here. If someone on the LDM is interested in championing it, it could happen. That said, given the costs here (and the easy solution of just using a raw-string instead), i imagine the motivation to pick it up will be low. Thanks! |
Beta Was this translation helpful? Give feedback.
There is no decision to disallow. The language works in the opposite direction. We decide what to allow. There was never any proposal or suggestion to allow this, so the concept has never even come up for discussion. :)
If you'd like this to be allowed, we can work through a proposal here. If someone on the LDM is interested in championing it, it could happen.
That said, given the costs here (and the easy solution of just using a raw-string instead), i imagine the motivation to pick it up will be low.
Thanks!