Propagation of CultureInfo to a nested interpolated string #6882
Unanswered
ufcpp
asked this question in
Language Ideas
Replies: 2 comments 3 replies
-
Beta Was this translation helpful? Give feedback.
1 reply
-
Wouldn't that the CustomHandler.Create(
CultureInfo.GetCultureInfo("fr-fr"),
$"direct: {1.5} or {1:C}, nested: {
If(condition, // cannot invoke AppendFormatted(CustomHandler handler) directy
$"{1.5}",
$"{1:C}")
}"); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
TL;DR
I'd like a mechanism to propagate
CultureInfo
to a nested interpolated string with the following code.Background
I am writing a utility library for string interpolation. One of the features provided by this library is the ability to do the following
However, this code has the following issue.
IFormatProvider propagation
Unfortunately, string manipulation in .NET mostly depends on
CurrentCulture
. There are many situations where this is not desirable, and the culture is required to be explicitly specified.For string interpolation, you would use
string.Create(culture, $"")
instead of$""
. However, theculture
explicitly passed here is not propagated to the nested interpolated string.For example, execute the following code:
The following results are obtained:
It would be a solution if
string.Create(culture)
were made explicit again in thenested
part, but human error inevitably occurs.Returning to my library,
If
andForEach
in this library propagateculture
over the argument of the lambda expressionb
. As a result, the boilerplate codeb => b.Append($"")
is required. This is cumbersome, and I would prefer to write it withoutb.Append
, as shown below.However, in the current version of C#, I can't think of a way to propagate
culture
in this style.A close implementation, as far as it can be written in C# 11, would be as follows.
Here is an excerpt:
The current implementation produces the following output. (Different for direct and nested.)
The expected output is rather like this
(Same with direct and nested.)
I am having trouble finding a way to do that in the current C#.
The problem is that there is no way to propagate the
CultureInfo
passed tostring.Create
to the handler of the argument of theIf
.Discussion
Now, the aforementioned
string.Create
code lowers to the following codeCan we somehow propagate
cultureInfo
to the nested handlers as follows?To achieve this, could we use a mechanism such as Implicit parameters?
"implicit hell" is scary, but IMHO, implicit parameters are somewhat better than relying on static properties (like
CurrentCulture
orSynchronizationContext.Current
)I would like to hear your opinions.
Beta Was this translation helpful? Give feedback.
All reactions