String interpolation, after a healthy debate on discord

deadalnix deadalnix at gmail.com
Thu Dec 16 13:19:16 UTC 2021


On Tuesday, 14 December 2021 at 21:06:19 UTC, Paul Backus wrote:
> On Tuesday, 14 December 2021 at 20:07:43 UTC, Daniel N wrote:
>> On Tuesday, 14 December 2021 at 16:15:28 UTC, WebFreak001 
>> wrote:
>>> ```d
>>> string name = readln();
>>> auto greeting = text!(__header!..., "hello ", name, "!");
>>> ```
>>>
>>> that wouldn't work because name is a runtime variable and you 
>>> are trying to use it as a template parameter here.
>>>
>>
>> I assumed it works with alias/variadic params...?
>>
>> ```d
>> import std;
>>
>> int sum(Vs...)()
>> {
>>     int sum = 0;
>>
>>     foreach(v ; Vs)
>>         sum += v;
>>
>>     return sum;
>> }
>>
>> void main()
>> {
>>     int a = to!int(readln());
>>     sum!a.writeln;
>> }
>> ```
>
> It works for variables, but not arbitrary expressions. For 
> example, if you wrote
>
> ```d
>     sum!(a, a+1).writeln;
> ```
>
> ...then you would get an error:
>
> ```
> Error: variable `a` cannot be read at compile time
> ```

So passing a raw a should work (it's an alias parameter).

Passing a+1 obviously doesn't as it is not a value that can be 
computed at compile time. Which lead me to think, can't we 
generate some kind of struct on the fly for the "${}" syntax? 
That would solve the problem and the end result would be 
supperior to the i solution (in fact, it would allow to implement 
the i solution as a library).


More information about the Digitalmars-d mailing list