-
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
Syntax to choose CurrentCulture/InvariantCulture for interpolated strings #3044
Comments
See also dotnet/roslyn#12298 |
The problem with However, I completely agree that the amount of code to use invariant culture is a bit too big. In case of interpolated strings, "using static" ( |
Just for interpolated strings, I'd propose an alternate prefix. Perhaps (This however doesn't solve the problem of having to specify |
String interpolation is so convenient I see people use it everywhere, even for
With the right
Although I don't like the suffix proposal, I would love to see a shorthand for
As for the syntax, I think it's better if we keep everything at the same place, i.e. at the start. Maybe EDIT: I think the |
How about using this syntax instead: $("count: {count}", CultureInfo.InvariantCulture) Where the second "argument" is an IFormatProvider. |
@dlemstra How is it better than the current |
With this syntax you would not need to add the "static using" in every file. And you can easily put it in one of the other IFormatProviders. It's not smaller but I don't think the goal of interpolated strings is to make it smaller. It was added so you can just put your arguments at the place where you use it instead of worrying about the order of your arguments when you do a string.Format. And with my syntax you can easily add the CultureInfo when you need it. |
It's counter-intuitive. Indeed it looks like error in source file. $"count: {count}"c // this will call string.Format with CultureInfo.CurrentCulture
$"count: {count}"i // this will call string.Format with CultureInfo.InvariantCulture
$"count: {count}" // as it is now, this will call string.Format without CultureInfo, effectively (...) This approach is much better and understandable from the first sight: $("count: {count}", CultureInfo.InvariantCulture) |
@Opiumtm adding dollar sign before the double-quote for interpolated string is also counter-intuitive if you look from the perspective of C# 5 :) |
I'd go with something like this:
The reason is the string can be lengthy and it might be annoying to look for, some people might miss it so for readability I'd go with a prefix and not postfix. :) |
@eyalsk 👍 |
Different in two aspects. First, This could be avoided if Nevertheless, with the proposed new syntax, if Second, the necessity of putting Altogether, this may look like a discussion about some petty syntax addition, but I believe we have inherent problem with interpolated strings that not that petty. @gafter I would really appreciate if you could take a stance here, since you were involved the other issues related to string interpolation that are now closed, but no improvement was made so far. Does C#/Roslyn team plan to provide us some workaround at least for allocation issue related to the use of |
@nanoant I haven't heard any proposals that fit the bill. If it is a change of language you're looking for, this belongs in the |
dup #177 |
Perhaps a syntax like below could be used in that it would use the one set right after $, and let any of the placeholder override the default per needs? This would align with the current overall syntax and enable in-string flexibility?
|
This was addressed in C# 10 with interpolated string handlers: |
Not quite the same kind of flexibility and syntactic sugar though... |
Meh, this doesn't address much as we already have a shorter version |
It is more painful to lose CultureInfo in nested interpolation rather than the length of the code. |
A related issue is that
The warning wouldn't even be required if there was a short symmetrical syntax
|
This requirement to me feels like it would need the exact same underlying mechanism as collection literals would for allowing the passing of constructor parameters. If a syntax such as the one I mentioned there was put in place, it would be general enough to apply to interpolation as well: $"{somevalue}"(CultureInfo.InvariantCulture) Or $"{somevalue}" with (CultureInfo.InvariantCulture) @TahirAhmadov This is why I was pushing so much for orthogonality in that other thread. Here we have another very similar situation where a common syntax would be greatly beneficial. You dismissed my suggested general syntax by saying this:
But here we are. This is basically the exact same problem, but with interpolated literals. A syntax that was specific for collection literals would not work here and would force a different syntax for passing constructor-like arguments. |
the idea of special syntax for these different feature areas is that htey benefit from dedicated syntax per-domain. For example, one could think of an string as a collection of characters. That doesn't mean the same syntax for collection-exprs is what we want for strings. When something is important enough, we actually go away from uniform syntax to specialized syntax. It's very possible that will be true here as well. IMO, for one, i absolutely do not want collection-exprs bogged down by a heavyweight syntax for arguments. The idea behind collection-exprs is for syntactic overhead to be at an absolute minimum. That's why, for example, we are not going with |
Well I can speak for myself that the reason I suggested the general syntax was not based on that at all... it was just to make sure the new "capability" of allowing to pass extra parameters for short-hand/literals was as orthogonal as possible in the language and could be used consistently in as many cases as possible. The fact it fits (IMHO) this case here is just a consequence of that, of it being orthogonal.
Fair enough, but I just don't see the reason to create dedicated syntax in this (these) particular cases when there are alternatives that are just as clean that apply globally.
Is this really heavyweight though? It's an extra parenthesis with the desired value... there are like 2 extra characters other than the actual intent there. $"{somevalue}"(CultureInfo.InvariantCulture) Yet here we have proposals for weird stuff like Maybe we could even go one step further and come up with something like "implicit static using" and allow this without an explicit $"{somevalue}"(InvariantCulture) Can you get more concise than that without being limiting and cryptic? |
@colejohnson66 were you trying to make some point? Can you elaborate? |
C# introduced interpolated string, but with no obvious way to decide which CultureInfo to use during formatting.
Currently it is possible to use InvariantCulture like this:
This requires code to target .NET 4.6 and allocates the FormatabbleString instance instead of just generating call to string.Format.
This could be improved by adding some syntax to explicitly choose CultureInfo, like:
This will compile to just string.Format and will not require targeting .NET 4.6.
Also it will be easy to add analyzer to enforce setting the
c
andi
suffixes always, like we have now in FxCop for string.Format and others.The text was updated successfully, but these errors were encountered: