-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Champion "Constant interpolated strings" #2951
Comments
As I understand it, the biggest problem with this feature is that the value of an interpolated string can depend on the current culture. How do you propose to solve that? In other words, if I had |
@svick |
From the notes:
|
@YairHalberstadt Tried it on C# interactive: > "" + 3.14
"3.14"
> System.Threading.Thread.CurrentThread.CurrentCulture =
. System.Globalization.CultureInfo.GetCultureInfo("id-ID");
. "" + 3.14
"3,14" |
@svick, we haven't come up with a full proposal yet, but in LDM yesterday we were of the opinion that you can't do that. You can only do things in |
What are the use cases of this feature? This feature seems of such minor use that I have no idea how it made it to an LDM. |
Building a constant string out of other constant, but allowing the user to use interpolations, instead of forcing them to use concatenation. |
It's a very slight difference, and the end result is the same. I was going to mention reusable URI components in routing attributes as a use case, but then I remembered that those attributes use curly braces to denote path parameters, and that could end up being really confusing. That might be an argument against this feature. |
IDE colorization should help with that @HaloFour. Right? |
Unless you also have an IDE extension to colorize URIs in ASP.NET routing attributes? 😄 |
That's saying how it works, not what the use cases are. I can probably count on one hand how many times I could've used a feature like this in the 10+ years I've been writing C#. I don't understand how such a minor feature that no one is asking for makes it to an LDM but related (and much more useful requests), like expanding the set of compile-time allowed expressions don't. What about using System.Math at compile time? What about reflection at compile time? All things people ask for ad-nauseam and that solve real problems. Why should string interpolation, which is just a fancy syntax sugar for stuff we can already do with concatenation at compile time, be the blessed operation that gets promoted? I'm not saying that any of those other things should be prioritized or not prioritized at this time either, it just continues to strike me how completely arbitrary the LDM prioritization continues to be. |
The use case is when i want to make a constant string and build it out of other constant strings. Formattable strings looks nicer and easier to read, so i would like to use those.
The LDM decides what they want to champion. It's that simple. :)
What about it?
What about it?
Ok. Convince an LDM member to take that up. It's not relevant in this proposal. :)
Because an LDM member championed it.
It is arbitrary. The LDM prioritizes what they think is most relevant. Sometimes that means that little features get picked because an LDM member thinks it is worth it (and possibly because they're willing to find the time to make it happen). |
I suspect I would use this most in // before
[DebuggerDisplay("{Foo}")]
// or maybe
[DebuggerDisplay("{" + nameof(Foo) + "}")]
// after
[DebuggerDisplay($"{{{nameof(Foo)}}}")] |
Another use case: [Obsolete($"Use {nameof(ILaunchSettingsProvider2)} instead.")] |
Yeah, attribute arguments were the place I thought this most useful, and have run into it before. I championed it because I happen to know that we implemented an optimization that basically turns an interpolated string into a string.Concat call not too long ago, so I bet this would be pretty easy to implement. |
@agocke the latest compiler doesn't just turn it into |
Yup, that's why I thought this would be pretty simple. Probably some stuff to mop up around constant folding, but overall shouldn't be too bad. |
FYI: |
@svick has a very fair and extremely important point. Culture dependency and unavoidable side effects that will be introduced by that is making me very wary about this. |
This feature won't work in this scenarios. It'll only work for interpolating other constant strings, so locale is not a concern. It's really just a slightly different syntax for string concatenation. |
@HaloFour in that case I retract my objection. Thanks for clearing that up, and sorry for missing that quote from the notes. |
Just for reference, this is a long-standing request. dotnet/roslyn#4678 was migrated from an internal Microsoft bug database in 2015. There are also a few issues in this repo related to this feature: I look forward to this simple and straightforward improvement to the ergonomics of string interpolation in C#. |
Can #3044 give benefits this proposal? |
when is 16.9. about to be rolled out? |
There's no public date I can share currently. |
The best place to look for VS release dates, RTM and preview, is here https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes |
of course, this page I am checking daily. Is there any site where release dates are listed in advance? It's nasty that I daily have to check if there is a new version for VS installer to update. Is there any possibility to get notified via email if a new version is available? This is annoying. |
VS itself does update checks, and should let you know with a popup inside VS if there is a new version available. |
well, sometimes this actually happens... in more than 90% of all new versions the new version was downloaded by myself before VS had any clue of it. not sure what the notification policy behind this is. |
Based on my own observations, I believe VS checks daily. |
Is it possible to allow alignment component in interpolation expression? const string TitleHeader = "Title";
const string CountHeader = "Count";
const string TableHeader = $"{TitleHeader,-50}|{CountHeader, 10}"; |
No, at least not as proposed and implemented. |
I think (maybe I'm wrong?) alignment can be implemented for |
You are free to do so. However, we explicitly rejected alignment from the feature in order to keep it simple and avoid any dependence on the implementation of string.Format or potential culture dependence. |
Thank you for the answer,
is enough for me for understanding. |
What happened to this feature? https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md#c-100 states it was merged into 16.9 yet VS 2019 is currently on 16.11 with const interpolated strings nowhere to be seen - is this coming to VS 2019 ever? Or will it (and C# 10 as a whole) be restricted to VS 2022? |
@IanKemp This feature is working for me in 16.11.2 at the moment. Remember, you have to specify |
Thanks for the reminder, for some reason I had it in my head that langver=latest would do it. I also don't see any mention of this feature in the release notes for 16.9.x/16.10.x/16.11.x, which honestly doesn't surprise me because this whole C# language version vs IDE version vs .NET Runtime version matrix is getting rather horribly complicated. I'm hoping you guys are going to perform something of a reset in this regard when you do the big bang drop of C# 10 + .NET 6 + VS 2022 in November. I mean, just looking at https://github.com/dotnet/roslyn/blob/main/docs/Language%20Feature%20Status.md#c-100 gives me anxiety. |
When c#10 drops, 'latest' will work. Until then, you need 'preview'. This seems same and sensible.
I disagree, and I'm an ideal world these can be decoupled. |
Will it solve the interpolated string boxing problem? eg: int a = 1;
return $"string {a}"; |
@jerviscui FYI, interpolation with non-string could not be constant because of formatter dependency. Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");
Console.WriteLine($"{1.2}"); // 1.2
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("fr-fr");
Console.WriteLine($"{1.2}"); // 1,2 |
@ufcpp Thank you! int a = 1;
return $"string {a.ToString()}"; |
Does the formatting of integers change across cultures? |
Integers are not allowed in constant interpolated strings for that reason. |
I was surprised to discover that |
Example:
The text was updated successfully, but these errors were encountered: