The state of string interpolation

Martin Tschierschke mt at smartdolphin.de
Fri Dec 7 10:45:12 UTC 2018


On Friday, 7 December 2018 at 07:53:14 UTC, Mike Franklin wrote:
> On Friday, 7 December 2018 at 01:47:13 UTC, H. S. Teoh wrote:
>
>> And yes, `mixin interp!"..."` is horribly verbose. But 
>> ostensibly we could abbreviate it to something like `mixin 
>> i!"..."`.
>
> Perhaps what's needed is a different mixin operator that is not 
> verbose and significantly distinguishes it from the template 
> instantiation operator.
>
> mixin i!"...";  // instead of this...  (too verbose)
> i!"...";        // or this... (looks too much like a template 
> instantiation)
> i#"...";        // we create a new mixin operator `#`
>
> Mike

When I tried - some time ago - to find a way to be able to just 
write

exho!"${name} you are app. ${age*365} days old";

instead of - when using scriptlike (DUB package)

import scriptlike;
...
writeln(mixin(interp!"${name} you are app. ${age*365} days old"));

(Sorry for the naming of the function was to distinguish it from 
echo.)

I defined an additional modul exho.d which
only defines an mixin sting:

module exho;

enum use_exho="auto mixinter(string x)(){return mixin(interp!x);}
auto exho(string x)(){return 
mixin(\"writeln(\"~interp!x~\")\");}";

Now in use it comes down to this:

import scriptlike;
import exho;
//Now you can write in  every scope you need it: mixin(use_exho);
// or mixinter! as shortcut for mixin(interp!...)
void main()
{
   auto name = userInput!string("Please enter your name");
   auto age = userInput!int("And your age");

   mixin(use_exho);
   exho!"${name} you are app. ${age*365} days old";
}

You still need "mixin" to get the definition in the right scope 
to define your shortcut in place.







More information about the Digitalmars-d mailing list