-
Notifications
You must be signed in to change notification settings - Fork 1.1k
proposal: generate and handle FormatStringTuples from extened interpolated strings #3765
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
Comments
I'm not sure what problem this is trying to solve. If I'm understanding this right, you could just create a function like (string template, object[] args) CreateFormattedString(string template, params object[] args) {
return (template, args);
} Instead of needing to add a whole new language feature. |
This sounds like something that could be solved by writing a Roslyn analyzer.
Why would standardization be useful here? Aren't these values usually passed directly to the logging method? |
You can't name the template elements (without custom format strings or something similar), but C# already essentially supports this with It looks like there have been requests to add support for
A few noteworthy things from the comments on that first issue: See also: dotnet/roslyn#142 -- An ancient language request to enhance |
Yes, the issue is ancient and i search/tried many things, but now had the idea with the new Tuples and interpolated strings.
A variant of my idea would be instead of generating an args array like in FormattableString to generate a named tuple var info = new { Name: "World", Number: 2 };
var formattedString = $("Hello {theName} {theNumber:D2}!", info.Name, info.Number);
//formattedString is: (Format: "Hello {0} {1:D2}!", theName: "World", theNumber: 2) but this has the issue that it cant be used in method parameters easily. |
dotnet 3.1 addresses this issue with the name support in message template in ILogger I still believed that without NLog or Serilog the standard ILogger would throw errors. |
Intro
The new interpolated strings feature works great to concatenate and format strings
but there is currently no support to work with FormatString-Template engines
like in Serilog or Elasticsearch (and many others).
Proposal
Instead of overloading multiple methods all over the place to push the parameters of
string template, object args[]
aroundc# could extend the interpolated strings syntax to generate a tuple
(string Template, object[] Args)
instead.The combination of tuple with interpolated strings could be used for new syntax extension
like
$("", arg1, arg2, ...)
.Benifite
The standardization and compile time validation of a formatted-string.
Pseudo code
final statement
It looks first not necessary, the tuple could be used directly or the tuple could be created over a helper function.
But both methods would still lack an compile time check of the template-string and a standardization for 3th party libs.
The text was updated successfully, but these errors were encountered: