Just another example of missing string interpolation

bachmeier no at spam.net
Thu Oct 12 18:00:46 UTC 2023


On Thursday, 12 October 2023 at 17:25:09 UTC, Commander Zot wrote:

> or like this:
>
> ```
> string enableInterpolation() {
>     return q{
>         string interp(string s)() {
>             import std.algorithm;
>             import std.meta;
>             import std.array;
>             import std.conv;
>             import std.functional;
>             enum spl = aliasSeqOf!(s.splitter(" ").array) ;
>
>             template P(string e) {
>                 static if(e.startsWith("$")) {           			
>                     mixin("enum P = 
> ()=>"~e[1..$]~".to!string;");
>                 }
>                 else {
>                     string fn(){ return e;}
>                     enum P = &fn;
>                 }
>             }
>
>             return [staticMap!(P, spl)].map!(f=>f()).join(" ");
>
>         }
>     };
> }
>
> int main() {
>     import std.stdio;
>
>     float speed = 42;
>     mixin(enableInterpolation());
>     writeln(interp!"i has $speed !");
>
>     return 0;
> }
>
> ```

I had something like that but moved away from it. The version I 
posted works better if I want to transform some of the variables. 
If I do that in a mixin, I have to parse the input. I actually do 
something like this using a struct:

```
string s = "String needing interpolation to insert ${y} and 
${x}.";
Subs subs;
subs["y"] = str.split("/")[0];
subs["x"] = 2.6*j;
interp(s, subs);
```

Again, though, this really should be done by the language rather 
than the user having to mess with it.


More information about the Digitalmars-d mailing list