String Interpolation

Andrea Fontana nospam at example.org
Wed Oct 25 15:55:07 UTC 2023


On Monday, 23 October 2023 at 14:12:25 UTC, Rumbu wrote:
> On Saturday, 21 October 2023 at 18:59:13 UTC, Adam D Ruppe 
> wrote:
>> On Saturday, 21 October 2023 at 18:31:10 UTC, Imperatorn wrote:
>>> In C# an interpolated string is just a string
>>
>> This isn't true. It is an object that can convert to string 
>> (similar to D's `alias toString this;`)
>
> In fact neither, it's syntactic sugar.
>
> ```csharp
> int count = 10;
> string s = $"I drink {count} beers";
> ```
>
> is syntactic sugar for:
>
> ```csharp
> int count = 10;
> DefaultInterpolationHandler dh = new 
> DefaultInterpolationHandler();
> dh.AppendLiteral("I drink ");
> dh.AppendFormatted(count);
> dh.AppendLiteral(" beers");
> string s = dh.ToStringAndClear();
> ```
>
> From compiler point of view, Roslyn stores in AST a 
> interpolated string as an array of expressions which get 
> converted before IL compilation to the code above.

I wonder why we can't do this in D, using text() or format() or 
everything else instead of returning tuples.

Andrea


More information about the Digitalmars-d mailing list