String interpolation, after a healthy debate on discord

WebFreak001 d.forum at webfreak.org
Tue Dec 14 12:55:30 UTC 2021


On Tuesday, 14 December 2021 at 12:41:27 UTC, Ogi wrote:
> On Tuesday, 14 December 2021 at 11:15:02 UTC, WebFreak001 wrote:
>> ah right you mean for the built-in istring usage in the 
>> compiler it needs to be implemented.
>>
>> I don't think it's such a big problem though, as the phobos 
>> implementation would also just be: (a little simplified)
>>
>> ```d
>> auto text(istring...)(istring s) if (isIString!istring)
>> {
>>     return text(s[1 .. $]); // removes special __header, just 
>> concatenate rest
>> }
>> ```
>
> [...]
>
> ```D
> //look ma no imports
> int bottleCount = 99;
> string str = i"$(bottleCount) bottles on the wall".stringof;
> assert(str == "99 bottles on the wall");

ah yeah I could see that work. I think it would only work at 
compile time though if you use .stringof (otherwise you need a 
runtime function - like text - that allocates the memory somehow, 
which is none of the compiler's business)

> My point is, if the compiler has to implement istring processor 
> anyway, why not make it a language feature:

Having the `text"..."` syntax IS a language feature, you just 
need an implementation, which phobos provides and is bundled with 
every distribution of D. (have it in std.conv or add it to object 
if you aren't interested in the other proposal to have default 
imports specified as compiler flags)


More information about the Digitalmars-d mailing list