Code block as template argument
Steven Schveighoffer
schveiguy at gmail.com
Tue Feb 11 15:51:31 UTC 2020
On 2/11/20 10:30 AM, Виталий Фадеев wrote:
> On Tuesday, 11 February 2020 at 14:58:58 UTC, Andrea Fontana wrote:
>> On Tuesday, 11 February 2020 at 11:34:49 UTC, Ali Çehreli wrote:
>>> On 2/11/20 3:14 AM, Виталий Фадеев wrote:> On Tuesday, 11 February
>>> 2020 at 11:10:44 UTC, Виталий Фадеев wrote:
>>>
>>> > Analog C-code with macros:
>>> > #define TPL(M,D,CODE) if ( state & M && diraction = D )
>>>
>>> ^^^^^^^^^^^^^
>>> [...]
>>>
>>> // Option 1: Equivalent of a C macro
>>> void executeMaybe(Func)(uint M, Direction D, Func func) {
>>> if ((state & M) && (direction == D)) {
>>> func();
>>> }
>>> }
>>>
>>> [...]
>>>
>>> Ali
>>
>> The C code apparently does an assignment inside the macro.
>
> Wow! Thank! I mass.
> I was mean comparation:
> Analog C-code with macros:
> #define TPL(M,D,CODE) if ( state & M && diraction == D ) CODE;
> example code
> TPL( OPEN, OUT,
> {
> delete AppsWindow
> state &= !OPEN
> }
> )
>
> It possible in D ?
> How pass code block to template ?
> How to solve this problem ?
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
More information about the Digitalmars-d
mailing list