You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: custom interpolated string handler, with the intent to perform bespoke operations (think "SQL auto parameterization")
If (and only if) all formatter tokens are constant strings, the string is reduced to a constant expression instead of using the expected AppendFormatted calls.
Version Used:
.NET 9.0.100 (current)
Steps to Reproduce:
The first Utils.Write here is the unexpected outcome; contrast this to a: the similar usage including a constant integer, and b: the similar usage using a variable string, or a variable string and variable integer (which all use AppendFormatted)
The first call uses Write(string) with the value concatenated at compile, as shown on sharplab.io
Expected Behavior:
Reduction occurs later; there's a question here as to whether it should still fold and prefer string in the case then the handler is a DefaultInterpolatedStringHandler and a similar string API exists.
Impact:
Custom in AppendFormatted scenarios are not reliable if the token turns out to be constant. In particular, it means that it is hard to see whether this resolves to DoThing(string) or DoThing(ref CustomInterpolatedStringHandler), which may have significant impact if the intent of CustomInterpolatedStringHandler is complex
obj.DoThing($""" some complex code with {Formatted} and non formatted {Could.Be.Far.Away.Regions} that may be miles away""");
(are Formatted and Regions constant expressions? we don't know, and neither does the reader)
Alternative Thought:
Instead of "fixing" this, maybe there should be a compiler warning if this could be a risk; perhaps all of:
an interpolated string is encountered
the interpolated string is being reduced to a constant expression by the compiler
the receiving argument is notstring or DefaultInterpolatedStringHandler
However, I guess that "3" depends on overload resolution having already occurred; maybe it should be
there is a competing usable overload that takes an interpolated string handler other than DefaultInterpolatedStringHandler
The text was updated successfully, but these errors were encountered:
Context: custom interpolated string handler, with the intent to perform bespoke operations (think "SQL auto parameterization")
If (and only if) all formatter tokens are constant strings, the string is reduced to a constant expression instead of using the expected
AppendFormatted
calls.Version Used:
.NET 9.0.100 (current)
Steps to Reproduce:
The first
Utils.Write
here is the unexpected outcome; contrast this to a: the similar usage including a constant integer, and b: the similar usage using a variable string, or a variable string and variable integer (which all useAppendFormatted
)Actual Behavior:
The first call uses
Write(string)
with the value concatenated at compile, as shown on sharplab.ioExpected Behavior:
Reduction occurs later; there's a question here as to whether it should still fold and prefer
string
in the case then the handler is aDefaultInterpolatedStringHandler
and a similarstring
API exists.Impact:
Custom in
AppendFormatted
scenarios are not reliable if the token turns out to be constant. In particular, it means that it is hard to see whether this resolves toDoThing(string)
orDoThing(ref CustomInterpolatedStringHandler)
, which may have significant impact if the intent ofCustomInterpolatedStringHandler
is complex(are
Formatted
andRegions
constant expressions? we don't know, and neither does the reader)Alternative Thought:
Instead of "fixing" this, maybe there should be a compiler warning if this could be a risk; perhaps all of:
string
orDefaultInterpolatedStringHandler
However, I guess that "3" depends on overload resolution having already occurred; maybe it should be
DefaultInterpolatedStringHandler
The text was updated successfully, but these errors were encountered: