[Proposal]: u8 string interpolation #9120
Replies: 4 comments
-
Probably worth explicitly calling out that this would only work for u8 strings that are being converted to a handler type, as there aren't currently plans to create a default handler type for them and that would require another spec change (what method is called to realize the byte array). |
Beta Was this translation helpful? Give feedback.
-
Right. And we won't be adding |
Beta Was this translation helpful? Give feedback.
-
It wouldn't just work since the result of the u8 string is not |
Beta Was this translation helpful? Give feedback.
-
I know. I think we're talking past each other. But it doesn't matter as without a new AppendLiteral overload on the default handler, it won't be relevant anyway. |
Beta Was this translation helpful? Give feedback.
-
u8 interpolated strings
Champion issue: #9121
Summary
Allow
u8
strings to be used with interpolation when the target handler has anAppendLiteral(ReadOnlySpan<byte>)
method.Motivation
In .NET 6, the
MemoryExtensions
class added aTryWrite
extension method that supports interpolating directly into a user-providedSpan<char>
:The literal portions of the string are directly copied, and the implementation recognizes the
ISpanFormattable
interface on the formatted values and prefers to use it in order to ask each value to write itself directly into the destination buffer.In .NET 8, dotnet/runtime plans to add a similar MemoryExtensions.TryWriteUtf8 method that can interpolate into a
Span<byte>
:This implementation recognizes the new
IUtf8SpanFormattable
interface on the formatted values and prefers to use it in order to ask each value to write itself directly into the destination buffer.However, as things currently stand, the string literal portions will end up being passed to
AppendLiteral(string)
calls, and the handler will need to UTF8 encode each at run-time, even though the data is known at compile-time. This can be avoided if the compiler encodes each literal at compile-time as it does foru8
literals:The above would be lowered exactly as it would without the
u8
, except each literal would result in a call toAppendLiteral(ReadOnlySpan<byte>)
, withu8
suffixed onto the literal:Detailed design
We allow the
u8
suffix on interpolated strings when targeting a handler. When supplied, anywhere the implementation would have looked for anAppendLiteral(string)
method on the handler in order to consider the handler valid and emit calls for the literal portions of the string, it instead looks forAppendLiteral(ReadOnlySpan<byte>)
.The C# 10 proposal for interpolated string handlers states "If there are any interpolated_regular_string_character components in i:
Member lookup on T with the name
AppendLiteral
is performed. The resulting method group is called Ml.The argument list Al is constructed with one value parameter of type string." If the
u8
suffix is employed, this would be changed to be "one value parameter of typeReadOnlySpan<byte>
". The arguments to thatAppendLiteral
method would be created as ifu8
had been appended to that individual literal.With a string literal, the handler's ctor is passed the length of the literal portion in chars. With a
u8
literal, the length would be in bytes (the sum of the lengths of all of theReadOnlySpan<byte>
s).Drawbacks
Alternatives
Unresolved questions
Design meetings
Beta Was this translation helpful? Give feedback.
All reactions