String Interpolation
duckchess
duckchess at chess.com
Mon Oct 23 00:49:54 UTC 2023
On Sunday, 22 October 2023 at 23:44:34 UTC, Adam D Ruppe wrote:
> On Sunday, 22 October 2023 at 22:57:30 UTC, duckchess wrote:
>> or improve the language, so we can express what we want in a
>> library.
>
> That's what the enhanced interpolation implementation does.
>
> https://github.com/adamdruppe/interpolation-examples
>
> Look down the examples here (and I have more coming as i find
> time to write them). How would you do them with your proposal?
```d
/** make this work **/
writefln(mixin(`InterpolationHeader!("", "greeting", ", ",
"name", "", "exclamation", "")(), "", greeting, ", ", name, "",
exclamation, ""`));
/** so it becomes this **/
writefln(InterpolationHeader!("", "greeting", ", ", "name",
"", "exclamation", "")(), "", greeting, ", ", name, "",
exclamation, "");
/************************/
/** then add smtn to D to automatically mix in strings at the
call side **/
mixin(string) t(string s)() {
// todo: code that actually generates the return string
return `InterpolationHeader!("", "greeting", ", ",
"name", "", "exclamation", "")(), "", greeting, ", ", name, "",
exclamation, ""`;
}
writefln(t!"$greeting, $name$exclamation");
/** turns into **/
writefln(mixin(`InterpolationHeader!("", "greeting", ", ",
"name", "", "exclamation", "")(), "", greeting, ", ", name, "",
exclamation, ""`));
/************************/
/** this is a sketch of how i'd like the default
implementation for i to look **/
mixin(string) i(string s)() {
return q{(){
struct RESULT {
string toString() const { return "....."}
mixin(string) printfargs(){ return ".....");
mixin(string) seq(){ return
`InterpolationHeader!("", "greeting", ", ", "name", "",
"exclamation", "")(), "", greeting, ", ", name, "", exclamation,
""`;}
}
return RESULT();
}()}
}
writeln(i!"$greeting, $name$exclamation");
writefln(i!"$greeting, $name$exclamation".seq);
```
again, idk if this specific idea works, but this is how i'd
imagine usercode to look like
```d
writeln(i!"Hello, $name! I liked DIP $dipNumber a lot.");
string result = i!"$name! See that DIP $dipNumber is easy to
convert to string.";
writefln(i!"$name has $$wealth:%0.2f".seq);
printf(i!"$name is $age years old.\n".printfargs);
writeln(tr!("I, $name, have a singular apple.", "I, $name, have
%d apples.", count));
writeln(tr!("I, $name, have a singular apple.", "I, $name, have
$count apples."));
```
More information about the Digitalmars-d
mailing list