Skip to content
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

Closed
bondsbw opened this issue Nov 23, 2015 · 6 comments
Closed

Extensible string interpolation #6972

bondsbw opened this issue Nov 23, 2015 · 6 comments
Labels
Area-Language Design Resolution-Answered The question has been answered

Comments

@bondsbw
Copy link

bondsbw commented Nov 23, 2015

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 of prefix$"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)):

var xml = XML$"<?xml version=""1.0""?>
    <contact>
      <name>Patrick Hines</name>
      <phone type=""home"">206-555-0144</phone>
      <phone type=""work"">425-555-0145</phone>
    </contact>";

and JSON (in #6673 (comment)):

var jsonString = JSON$"
    {
        ""contact"":
        {
            ""name"": ""Patrick Hines"",
            ""phones"": 
            [
                {
                    ""type"": ""home"",
                    ""number"": ""206-555-0144""
                },
                {
                    ""type"": ""work"",
                    ""number"": ""425-555-0145""
                }
            ]
        }
    }";

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.

@gafter
Copy link
Member

gafter commented Nov 23, 2015

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 MySpecialFormatting($@"stuff to be formatted specially") it will get formatted according to your custom IFormatProvider.

@bondsbw
Copy link
Author

bondsbw commented Nov 23, 2015

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.

@gafter
Copy link
Member

gafter commented Nov 23, 2015

@bondsbw You can put whatever you want inside the MySpecialFormatting method. It has complete control of the formatting of the string and its inserts. You can even have it return some type other than string.

@bondsbw
Copy link
Author

bondsbw commented Nov 23, 2015

@gafter Understood, but you may be missing my point.

There is no way for Xml($@"<contact><name>Patrick Hines</name>") to create a compile-time error when the enclosed XML string is not correctly formatted (missing </contact>) ... is there? Nor can it apply syntax highlighting.

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.

@axel-habermaier
Copy link
Contributor

@bondsbw: That's what analyzers are there for. Such an analyzer would check the code for invocations of your Xml function and then check whether the provided string is valid XML.

I don't know how extensible code completion and syntax highlighting are; in any case, they would probably require a VS extension.

@VBAndCs
Copy link

VBAndCs commented Apr 7, 2019

This proposal is similar but with different syntax: #34821

@dotnet dotnet locked as off-topic and limited conversation to collaborators Apr 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area-Language Design Resolution-Answered The question has been answered
Projects
None yet
Development

No branches or pull requests

4 participants