String Interpolation

Arafel er.krali at gmail.com
Mon Oct 23 08:28:57 UTC 2023


On 23/10/23 3:13, Walter Bright wrote:
> On 10/22/2023 4:32 AM, Arafel wrote:
>> At which point it's barely an improvement overs what we can already do:
>>
>> ```
>> int var = 67; auto s = mixin(i!"whatever $var");
>> ```
>>
>> I'm not saving even one keypress, and readability isn't that much 
>> better either.
> 
> If you can make `i` work as a template, why not? I encourage you to give 
> it a try and write the template!
> 
> I've already worked out the logic for this:
> 
> https://github.com/dlang/dmd/pull/15722/files#diff-a556a8e6917dd4042f541bdb19673f96940149ec3d416b0156af4d0e4cc5e4bdR14291
> 
> Feel free to turn it into a template, and let's see.

First of all, there are already dub projects that do this [1,2] for the 
simple case that I mentioned of getting a string as end result.

But if you want the same flexibility that your DIP offers, here is a 
small proof of concept of how you can get a tuple to be used in an "-f-" 
function with a mixin:

```d
import std;

enum i(string T) = "imported!\"std.meta\".AliasSeq!(\"%s: %s\",\"" ~ T ~ 
"\"," ~ T ~ ")";

void main() {
     int var = 5;
     writefln(mixin(i!"var"));
     // var: 5
}
```

This is a just to show that it **can** be done.

Implementing advanced parsing logic that generates whatever AliasSeq you 
want based on the input string instead of hardcoding it assuming it's 
just a variable name shouldn't be conceptually hard, especially not if 
it already exists, just perhaps tedious.

[1]: https://code.dlang.org/packages/stri
[2]: https://code.dlang.org/packages/jdiutil


More information about the Digitalmars-d mailing list