String interpolation, after a healthy debate on discord
Paul Backus
snarwin at gmail.com
Tue Dec 14 21:06:19 UTC 2021
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
```
More information about the Digitalmars-d
mailing list