dotnet / csharplang

The official repo for the design of the C# programming language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[API Proposal]: InterpolatedStringHandler allow method overload that has the "name" of the interpolated values.

LokiMidgard opened this issue · comments

Background and motivation

In some casese it may be of relevance to know from where the value was recived.

E.g.: A structual logger may want to know the variable to use when logging.

API Proposal

public void AppendFormatted<T> (T value, int alignment, string? format, string name);

name will contain the text wiritten in source code.

API Usage

subject = "Mail";
logger.LogInformation($"Create entity {subject}");

will call public void AppendFormatted<T> (T value, int alignment, string? format, string name); with
T: string
value:: "Mail"
alignment: 0
format: null
name: "subject"

Alternative Designs

public void AppendFormatted<T> (T value, int alignment, string? format, System.Linq.Expression name);

An alternateive, or additon could be to use Expressions to get more information.

Risks

Besides an increased complexexty I currently see no risk.
I can't anticipate the performance costs if Expressiontrees are used, but for the string variant I don't expect a high cost.