DIP 1027---String Interpolation---Community Review Round 1

Jacob Carlborg doob at me.com
Tue Dec 17 09:20:06 UTC 2019


On Monday, 16 December 2019 at 12:55:17 UTC, Patrick Schluter 
wrote:

> Yes, probably. The issue I had with the transformation as 
> string is what should the code below print?
>
> import fn;
>
> int a, b = 20;
>
> string inter = i"$(a+b)";
>
> for(a=0; a<10; a++)
>    fn(inter);
>
>  ---
>  module fn;
>
>  void fn(string s)
>  {
>    writef(s);
>  }
>
> prints
> 202020202020202020
>
> or
>
> 212223242526272829
>
> and that's the difference between CT evaluation and RT 
> evaluation.

It's difficult to say as your code doesn't compile. As it's 
written the code will fail to compile because you cannot have a 
for-loop at module scope. The line where `inter` is declared 
would fail to compile as well because `a` and `b` cannot be read 
at compile time. I don't see how that would be any different 
compared to this code:

module foo;
int a, b = 20;
int c = a + b; // fails to compile as well

If all of this code would be wrapped in a function, then it would 
successfully compile. `inter` would be evaluated to "20" and the 
code would print:

20202020202020202020

I don't see how it would behave any differently than if you 
replaced `string` with `int` and removed `i"$"`.

For the result to be `212223242526272829` `i"$(a+b)"` would need 
to be a macro.

--
/Jacob Carlborg



More information about the Digitalmars-d mailing list