Jump to page: 1 2
Thread overview
February 11
I want template this code

    if ( state & INIT && diraction = IN )
    {
      Label
        text = "text"
    }

    if ( state & OPEN && diraction = OUT )
    {
      delete AppsWindow
      state &= !OPEN
    }

to

    TPL( INIT, IN,
      {
        Label
          text = "text"
      }
    )


    TPL( OPEN, OUT,
      {
        delete AppsWindow
        state &= !OPEN
      }
    )


Desctiption:
condition: "if ( state & OPEN && diraction = OUT )"
replace to template: "TPL( M, D )"
pass code block as argument.

Analog C-code with macros:
    #define TPL(M,D,CODE) if ( state & OPEN && diraction = OUT ) 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 ?


February 11
On Tuesday, 11 February 2020 at 11:10:44 UTC, Виталий Фадеев wrote:
> I want template this code
>
>     if ( state & INIT && diraction = IN )
>     {
>       Label
>         text = "text"
>     }
>
>     if ( state & OPEN && diraction = OUT )
>     {
>       delete AppsWindow
>       state &= !OPEN
>     }
>
> to
>
>     TPL( INIT, IN,
>       {
>         Label
>           text = "text"
>       }
>     )
>
>
>     TPL( OPEN, OUT,
>       {
>         delete AppsWindow
>         state &= !OPEN
>       }
>     )
>
>
> Desctiption:
> condition: "if ( state & OPEN && diraction = OUT )"
> replace to template: "TPL( M, D )"
> pass code block as argument.
>
> Analog C-code with macros:
>     #define TPL(M,D,CODE) if ( state & OPEN && diraction = OUT ) 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 ?

Version 2 of question. more accurate.

I want template this code

    if ( state & INIT && diraction = IN )
    {
      Label
        text = "text"
    }

    if ( state & OPEN && diraction = OUT )
    {
      delete AppsWindow
      state &= !OPEN
    }

to

    TPL( INIT, IN,
      {
        Label
          text = "text"
      }
    )


    TPL( OPEN, OUT,
      {
        delete AppsWindow
        state &= !OPEN
      }
    )


Desctiption:
condition: "if ( state & OPEN && diraction = OUT )"
replace to template: "TPL( M, D, CODE )"
pass code block as argument.

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 ?

February 11
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 ) CODE;
> example code
>      TPL( OPEN, OUT,
>        {
>          delete AppsWindow
>          state &= !OPEN
>        }
>      )
>
> It possible in D ?

As a friendly reminder, the Learn newsgroup (or its Forum interface at https://forum.dlang.org/group/learn) is more appropriate for such questions.

Two options come to mind:

uint state;
enum uint OPEN = 0x2;
enum Direction { D, OUT }
Direction direction;

// Option 1: Equivalent of a C macro
void executeMaybe(Func)(uint M, Direction D, Func func) {
  if ((state & M) && (direction == D)) {
    func();
  }
}

// Option 2: Equivalent of a C struct
struct MaybeExecutor {
  uint M;
  Direction D;

  this (uint M, Direction D) {
    this.M = M;
    this.D = D;
  }

  auto opCall(Func)(Func func) {
    if ((state & M) && (direction == D)) {
      func();
    }
  }
}

void main() {
  int* AppsWindow;

  executeMaybe(OPEN, Direction.OUT, {
      destroy(*AppsWindow);
      state &= !OPEN;
    });

  auto ex = MaybeExecutor(OPEN, Direction.OUT);
  ex({
      destroy(*AppsWindow);
      state &= !OPEN;
    });
}

Ali


February 11
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:
>
> >      [...]
> CODE;
> > [...]
>
> [...]


Thank!
We love small beauty code. Smaller is better. Readable is better.
Like this:
> >      TPL( OPEN, OUT,
> >        {
> >          delete AppsWindow
> >          state &= !OPEN
> >        }
> >      )

February 11
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.

February 11
On 2/11/20 6:58 AM, Andrea Fontana wrote:

>> > Analog C-code with macros:
>> >      #define TPL(M,D,CODE) if ( state & M && diraction = D )
>>
>>                                                ^^^^^^^^^^^^^

> The C code apparently does an assignment inside the macro.
> 

I still think it's a typo. :)

Ali


February 11
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 ?


Sorry. How to delete thread ?

February 11
On Tuesday, 11 February 2020 at 15:08:11 UTC, Ali Çehreli wrote:
> On 2/11/20 6:58 AM, Andrea Fontana wrote:
>
>>> > Analog C-code with macros:
>>> >      #define TPL(M,D,CODE) if ( state & M && diraction = D )
>>>
>>>                                                ^^^^^^^^^^^^^
>
>> The C code apparently does an assignment inside the macro.
>> 
>
> I still think it's a typo. :)
>
> Ali

Thank. You understand me rigth, but your presented code too big.
We love simple, beauty.
February 11
On 2/11/20 2:39 PM, Виталий Фадеев wrote:
> We love small beauty code. Smaller is better. Readable is better.

How the following:
```D
TPL(OPEN, Direction.OUT,
      {
        AppsWindow);
	state &= !OPEN;
      }
    );
```C
is larger and less readable than
```
TPL( OPEN, OUT,
      {
        delete AppsWindow
        state &= !OPEN
      }
    )
```

Yes, this
```
void TPL(Func)(uint M, Direction D, Func func) {
  if ((state & M) && (direction == D)) {
    func();
  }
}
```
takes a little bit more line than
```
#define TPL(M,D,CODE) if ( state & M && diraction = D ) CODE;
```

But this gives you much more abilities - for example in D this code is processed by the compiler and the compiler gives you much more informative errors, for example. Хотя, конечно, на вкус и цвет, товарища нет.
February 11
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
« First   ‹ Prev
1 2