Replies: 17 comments
-
I like what is done in ECMAScript 6 and I would like to purpose a slightly different syntax than what is written above. How about the following syntax (with a backtick instead of the $):
This syntax will also make is easier to use a quote inside the string because you no longer need to replace them:
|
Beta Was this translation helpful? Give feedback.
-
String interpolation already uses |
Beta Was this translation helpful? Give feedback.
-
On syntax, the leading |
Beta Was this translation helpful? Give feedback.
-
@dlemstra from The C# Programming Language
I can type a backtick on my keyboard (Danish QWERTY) but it is a dead key which means I have to type This most likely takes the prize as the common programming character most difficult for me to type. |
Beta Was this translation helpful? Give feedback.
-
@alrz implementing the flag as a postfix will affect the most important lexer, the human lexer 😀. If your string is long enough you'll have to scan till its end to find out if the treatment is invariant. It can already be annoying with regexps, but regexps are (usually) short enough for this to work. |
Beta Was this translation helpful? Give feedback.
-
Yeah, I like the prefix |
Beta Was this translation helpful? Give feedback.
-
Does it really require to 'scan' the string? I'd rather say peek at the end of line ;) Regex is a reason why I like the idea of postfix. People may be used to this and expect modifiers to be at the end. Also |
Beta Was this translation helpful? Give feedback.
-
Do you mean Just because we have modifiers for numbers and regex at the end that in many cases tend to be short doesn't mean we need to follow the same pattern here. |
Beta Was this translation helpful? Give feedback.
-
@eyalsk I'd prefer |
Beta Was this translation helpful? Give feedback.
-
I don't know if adding more prefixes solves anything. There's also another problem that has to do with localization. No change: With localization support And with Of course I have a feeling that both problems can be solved in the same way. |
Beta Was this translation helpful? Give feedback.
-
@rikimaru0345 The other really good use case for |
Beta Was this translation helpful? Give feedback.
-
Maybe the following could solve the performance problems easily and in a fully backwards compatible way. The same code that decides whether to generate FormattableString or a string.Format call could be used to also potentially generate this:
Now you can easily write a method that takes all those parameters from the deconstruction and provide your own formatter like this:
Does that idea make sense? @jnm2 Unfortunately I don't know what can be done about const string literals. |
Beta Was this translation helpful? Give feedback.
-
@rikimaru0345 Yes, which is fine if all inputs are constants and the culture is invariant. |
Beta Was this translation helpful? Give feedback.
-
@jnm2 Yup I'm up for whatever syntax tbh as long as they will add it. p.s. Thanks, been busy with some projects and uni so I'm less active. 😉 |
Beta Was this translation helpful? Give feedback.
-
Allowing shorter symmetrical syntax would be nice from Globalization analyzer perspective: dotnet/runtime#88902 |
Beta Was this translation helpful? Give feedback.
-
The string result = string.Create(CultureInfo.InvariantCulture, $"count: {count}"); It utilizes the interpolated string handlers support introduced in C# 10, where the only allocation here will be for the resulting string. |
Beta Was this translation helpful? Give feedback.
-
@stephentoub I think the current state is not compatible with Globalization rules. Analyzers have to ignore interpolated strings because there is no way to specify the culture explicitly there. Users just have to know that CurrentCulture is used by default and analyzers have to assume it was intentional. I think it is a bit of a gotcha for new users and goes against the goal of the Globalization analyzers (at least as I understand them). Aside from analyzers, just ergonomically, it feels very strange to have such an asymmetric (for lack of a better word) syntax:
Invariant syntax feels very verbose. Is the assumption that the code that wants to use InvariantCulture should just set CurrentCulture to InvariantCulture (via e.g. Thread.CurrentCulture or System.Globalization.Invariant)? |
Beta Was this translation helpful? Give feedback.
-
Disclaimer: I've copy/paste this from #12336 but did some modifications.
Summary
Add syntax support to
CurrentCulture
/InvariantCulture
for interpolated strings.Motivation
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.Detailed design
This could be improved by adding prefixes to explicitly choose
CultureInfo
like this:This will compile to
string.Format
and will not require targeting .NET 4.6.Additionally, it will be easy to add analyzer to enforce setting the
C
andI
prefixes always, like we have now in FxCop for string.Format and others.p.s. I think that capital letters standout better but maybe lowercase letters might be slightly easier to type.
Beta Was this translation helpful? Give feedback.
All reactions