-
Notifications
You must be signed in to change notification settings - Fork 4.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
Extensible string interpolation #6972
Comments
This is already supported by the existing string interpolation feature, though not with the precise syntax you requested. private static IFormatProvider CustomFormatter = /* Your custom formatter here */;
string MySpecialFormatting(System.FormattableString f) => f.ToString(CustomFormatter); then, when you write |
Does that implementation allow for syntax highlighting and the other aspects (automatic formatting, statement completion, compilation errors and warnings)? All I see that it does is to provide a way to extend how the string constant is transformed to the output. |
@bondsbw You can put whatever you want inside the |
@gafter Understood, but you may be missing my point. There is no way for To be clear, I'm ok with that syntax... any decent syntax is fine to me. I'm just not seeing that the same functionality exists. |
@bondsbw: That's what analyzers are there for. Such an analyzer would check the code for invocations of your I don't know how extensible code completion and syntax highlighting are; in any case, they would probably require a VS extension. |
This proposal is similar but with different syntax: #34821 |
Proposal
Provide a mechanism in a future version of C# for writing extensions to the string interpolation capabilities introduced in C# 6.
Purpose
To extend C# to host well-formed strings of other languages, such as XML and JSON, without tying C# to any specific format (as noted as the reason for not including XML literals in the language in #1746 (comment) and #3912).
The extension would provide syntax highlighting, code formatting, statement completion, compilation errors and warnings, and intellisense.
Mechanism
C# 6 string interpolation introduced the
$"string"
syntax. I propose that in a future version of C#, a prefix is added to the current string interpolation syntax in the form ofprefix$"string"
.prefix
would map to a Roslyn-based extension.Examples
I mentioned this syntax before in other issues, and provided examples which I will repeat here. XML (in #1746 (comment)):
and JSON (in #6673 (comment)):
This could be extended to other formats and languages such as
YAML$
,regex$
,XAML$
,fsharp$
, and so on.Outside of scope
This proposal only extends to an output type of
string
. Extending to dictionary or array initialization (for example, #6673) or DOM initialization is out of the scope of this proposal. Extension methods and APIs may be suitable for those cases, or perhaps other proposals for extending the language in a similar fashion.EDIT: moved the statement about syntax highlighting/etc. to the Purpose section.
The text was updated successfully, but these errors were encountered: