Playing arround with mixin - alias?
Martin Tschierschke via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 21 04:58:13 PDT 2017
On Friday, 21 April 2017 at 10:31:46 UTC, ketmar wrote:
> biozic wrote:
>
>> On Friday, 21 April 2017 at 09:42:33 UTC, ketmar wrote:
>>> Martin Tschierschke wrote:
>>>
>>>> Is it possible to define an alias for something like
>>>>
>>>> mixin(import("local_function_file.d"));
>>>>
>>>> to write only
>>>>
>>>> use_local_function;
>>>>
>>>> which will be translated to:
>>>> mixin(import("local_function_file.d"));
>>>>
>>>> (this additionally needs the file local_function_file.d in
>>>> source/ +
>>>> -J./source as parameter for dmd aka. dflags "-J./source" in
>>>> dub.sdl)
>>>>
>>>> Regards mt.
>>>
>>> nope. the best you can get is `mixin use_local_function;`.
>>
>> Would there be an advantage compared to a simple `import
>> local_function_file;`?
>
> i doubt so.
So my "solution" on how to get a short cut for using:
writeln(mixin(interp!"${name} you are app. ${age*365} days old"));
// first place in source/exho.d the following code:
auto mixinter(string x)(){return mixin(interp!x);}
auto exho(string x)(){return mixin("writeln("~interp!x~")");}
later compile this with -J./source
source/app.d:
import scriptlike;
auto insert(string name)(){return import(name);}
alias insert!"exho.d" use_exho;
void main()
{
auto name = userInput!string("Please enter your name");
auto age = userInput!int("And your age");
//Now you can write in every scope you need it:
mixin(use_exho);
exho!"${name} you are app. ${age*365} days old";
// Which looks little nicer than:
// writeln(mixin(interp!"${name} you are app. ${age*365} days
old"));
// or if you need the result in a var:
mixin(use_exho);
auto var = mixinter!"${name} you are app. ${age*365} days old";
writeln(var);
}
you may even name the alias just exho and write:
alias insert!"exho.d" exho;
mixin(exho);
exho!"${name} you are app. ${age*365} days old";
(exho was derived from shell echo with mi_x_in)
More information about the Digitalmars-d-learn
mailing list