Code block as template argument

Виталий Фадеев vital.fadeev at gmail.com
Tue Feb 11 16:03:55 UTC 2020


On Tuesday, 11 February 2020 at 15:51:31 UTC, Steven 
Schveighoffer wrote:
> On 2/11/20 10:30 AM, Виталий Фадеев wrote:
>> [...]
>
> D does not have macros. Instead we have mixins. mixins allow 
> you to write code as a string, and then interpret the code as 
> if it were typed in the given location.
>
> For example (and I'm not sure how you want to deal with M and 
> D, this is one possibility):
>
> string TPL(size_t M, SomeType D, string block) { // not sure 
> what type of D should be
>
> return "if((state & " ~ M.to!string ~ " && diraction == " ~ 
> D.to!string ~ ") " ~ block;
> }
>
> // usage:
>
> mixin TPL(OPEN, OUT,
> q{
>     delete AppsWindow;
>     state &= !OPEN;
> });
>
> -Steve
Steve, it cool! Thank!


More information about the Digitalmars-d mailing list