Closed as not planned
Description
The following code throws a stack overflow exception.
var formatter = new SpanFormatter();
formatter.AddFormatter<int>(x => $"Int: {x}");
Span span = formatter.Format(42);
The issue is the SpanFormatter recurses on the parameter, and each time looks up the formatter, which causes it to loop back on itself.
If you specify a format parameter the problem no longer occurs:
var formatter = new SpanFormatter();
formatter.AddFormatter<int>(x => $"Int: {x:F0}");
Span span = formatter.Format(42);
This is due to the different code path that handles the format.
We also need to consider how to handle this in the generic case. Consider what happens if a format specifier is declared on string
.
var formatter = new SpanFormatter();
formatter.AddFormatter<string>(x => $"Prefix: {x}");
Span span = formatter.Format("42");