Just another example of missing string interpolation

bachmeier no at spam.net
Thu Oct 12 21:45:55 UTC 2023


On Thursday, 12 October 2023 at 19:42:41 UTC, kdevel wrote:
> On Thursday, 12 October 2023 at 16:31:39 UTC, bachmeier wrote:
>> [...] I use a generalization of this:
>>
>> ```
>> string interp(string s, string[string] subs) {
>>   foreach(k; subs.keys) {
>>     s = s.replace("${" ~ k ~ "}", subs[k]);
>>   }
>>   return s;
>> }
>> ```
>
> What if `s` is `"${a}"` and `subs` is `["a": "${b}", "b": "x"]`?

That's the reason it needs to be supported by the language.

What I do in practice is replace `${` in `subs[k]` with 
`ControlChar.us`, then convert `ControlChar.us` to `$` before 
returning the final result. That's because I would never have 
`ControlChar.us` in a string with interpolation, and I don't have 
to worry about efficiency because it's just not an issue. Your 
example is something I don't have to worry about very often. This 
is in no way a solution that would be suitable for the compiler 
or Phobos. It's a hack that works for me.


More information about the Digitalmars-d mailing list