Skip to content

API Proposal: C# 10 interpolated strings support, part 3 #51962

Closed
@333fred

Description

@333fred

Background and Motivation

Part 1 and part 2 added APIs to support the improved interpolated string literal feature in C# 10. After a bit more design in the space, we want to make a few minor changes to the names and add a new attribute to help improve the authoring and usage experience for users.

The general driving goal here was to standardize the names builder types use. It is expected that our guidance to API authors will be "Your interpolated string builder types should start with the words InterpolatedString and end with Builder". This convention will help make it clearer for users when something is a valid builder type.

Updated API: InterpolatedStringDefaultBuilder

We rename InterpolatedStringBuilder to InterpolatedStringDefaultBuilder. This is being done to avoid confusion with the new attribute.

-    public ref struct InterpolatedStringBuilder
+    public ref struct InterpolatedStringDefaultBuilder

Proposed API: InterpolatedStringBuilderAttribute

This attribute marks a type as being a valid builder type. This is a change from the existing proposal, where the validity of the conversion was being determined from doing a bunch of overload resolution. This should make errors more sane and helpful to end users.

Open question: Should we allow this on interfaces as well? With abstract statics in interfaces, it would theoretically be possible to use type parameter constrained to an interface as a builder type, but that would only be valid if the interface can be attributed with this.

namespace System.Runtime.CompilerServices
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
    public sealed class InterpolatedStringBuilderAttribute : Attribute
    {
        public InterpolatedStringBuilderAttribute()
        {
        }
    }
}

We then update the structs defined in Parts 1 and 2 as follows, to add the attribute and also standardize the naming of the other struct types with the rules mentioned above:

namespace System.Runtime.CompilerServices
{
+   [InterpolatedStringBuilder]
    public ref struct InterpolatedStringDefaultBuilder ...

+   [InterpolatedStringBuilder]
-   public ref struct InterpolatedSpanBuilder ...
+   public ref struct InterpolatedStringSpanBuilder ...
}

namespace System.Text
{
    public sealed class StringBuilder
    {
+       [InterpolatedStringBuilder]
-       public struct InterpolatedAppendFormatBuilder ...
+       public struct InterpolatedStringAppendFormatBuilder ...
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions