-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Proposal: String interpolation via AppendFormat pattern #28945
Comments
I don't think translating a single call to method
Currently, interpolated strings convert to Also, I think it would be nice if interpolated strings supported something like structured logging. So, if I call something like
This is not possible today with A new representation for interpolated strings might be a good opportunity to enhance them in this way, or at least to make sure the proposed design does not prevent doing this in the future. |
Console.WriteLine would need either to have an overload accepting the target (e.g. the ValueStringBuilder) and it would write the newline into it, or you would use Console.Write rather than Console.WriteLine and append the newline yourself (ideally with a Write overload accepting a span to avoid the trip through a string), or it could do two separate writes to the console. At the end of the day, this approach only gets you as far as someone using the thing that's building up the result... there will often be some point at which you convert that result into whatever the target is (e.g. a string, or a span), after which any additional manipulation/allocations are beyond your control.
Not necessarily. @jaredpar is already proceeding on a path here where the compiler could prefer a new type over string. |
Indeed. The proposal is being tracked here In summary though: if we can find a good win and an explainable back compat case then we should be able to move forward here with a change. |
Replaced by #50601 |
Copied from @stephentoub https://github.com/dotnet/corefx/issues/35806#issuecomment-470310193
One of the main use cases this is being proposed for is around string interpolation and string formatting.
I realize there are other uses cases, so not necessarily instead of a something Variant-like, but specifically to address the case of string interpolation, I had another thought on an approach….
Today, you can define a method like:
and use that as the target of string interpolation, e.g.
Imagine we had a pattern (or an interface, though that adds challenge for ref structs) the compiler could recognize where a type could expose a method of the form:
The type could expose additional overloads as well, and the compiler would use normal overload resolution when determining which method to call, but the above would be sufficient to allow string interpolation to be used with the type in the new way. We could add this method to StringBuilder, for example, along with additional overloads for efficiency, e.g.
We could also define new types (as could anyone), as long as they implemented this pattern, e.g.
Now, when you call:
rather than generating what it would generate today if this took a FormattableString:
or if it took a string:
it would instead generate:
There are more calls here, but most of the parsing is done at compile time rather than at run time, and a type can expose overloads to allow any type T to avoid boxing, including one that takes a generic T if so desired.
Copied from @JeremyKuhne https://github.com/dotnet/corefx/issues/35806#issuecomment-470365126
@stephentoub Generally speaking I like the idea of moving parsing to compile time. I'll play around to see what sort of perf implications it has.
One thing I'd want to make sure we have an answer for is how do we fit
ValueFormatableString
(or something similar) into this picture? Ideally we can add just one overload toConsole.WriteLine()
that will magically suck$""
away fromConsole.WriteLine(string)
. Could we leverageValueStringBuilder
for this?We could also add overloads that take
IFormatProvider, ValueStringBuilder
? Or possibly just add an optionalIFormatProvider
onValueStringBuilder
? Then something like this could happen:Copied from @stephentoub https://github.com/dotnet/corefx/issues/35806#issuecomment-470392810
This is one of the advantages I see to the aforementioned AppendFormat approach. In theory you just have another
AppendFormat(ReadOnlySpan<char> value, ReadOnlySpan<char> format)
overload, and then you could do$"This contains a {string.AsSpan(3, 7)}"
and have that "just work".The text was updated successfully, but these errors were encountered: