Interpolated strings
Martin Tschierschke via Digitalmars-d
digitalmars-d at puremagic.com
Thu Apr 20 03:23:30 PDT 2017
On Wednesday, 19 April 2017 at 17:51:05 UTC, Nick Sabalausky
(Abscissa) wrote:
> On 04/17/2017 03:41 PM, Jonas Drewsen wrote:
[...]
>>> exho!"The number ${num} doubled is ${num * 2}!"
> Also, it only works if you're just sending the string to
> writeln. It doesn't help the general case :(
you can define:
auto mixinter(string x)(){return mixin(interp!x);}
(in the same scope as the vars used inside x)
And use:
string text = mixinter!"${name} and this are app ${age*365*24}
hours!";
But can someone explain me why this works:
import scriptlike;
void main()
{
auto name = userInput!string("Please enter your name");
auto age = userInput!int("And your age");
// The proposed notation for scriplike string interpolation
writeln(mixin(interp!"${name} you are app. ${age*365} days
old"));
// with exho definition
auto exho(string x)(){return mixin("writeln("~interp!x~")");}
exho!"${name} and this are app ${age*365*24} hours!";
}
and this not?
import scriptlike;
// with exho definition outside the scope of the used vars
auto exho(string x)(){return mixin("writeln("~interp!x~")");}
void main()
{
auto name = userInput!string("Please enter your name");
auto age = userInput!int("And your age");
writeln(mixin(interp!"${name} you are app. ${age*365} days
old"));
exho!"${name} and this are app ${age*365*24} hours!";
}
Can it be possible to allow a function to be defined outside the
scope of use to return a "mixin object"?, than everything can be
done in a lib outside, no need to add parsing complexity to the
language?
More information about the Digitalmars-d
mailing list