www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Code block as template argument

reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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 ?
Feb 11 2020
parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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 ?
Feb 11 2020
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/11/20 3:14 AM, =D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 =D0=A4=D0=B0=
=D0=B4=D0=B5=D0=B5=D0=B2 wrote:> On Tuesday, 11 February 2020=20
at 11:10:44 UTC, =D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 =D0=A4=D0=B0=D0=
=B4=D0=B5=D0=B5=D0=B2 wrote:

 Analog C-code with macros:
      #define TPL(M,D,CODE) if ( state & M && diraction =3D D ) CODE;
 example code
      TPL( OPEN, OUT,
        {
          delete AppsWindow
          state &=3D !OPEN
        }
      )

 It possible in D ?
As a friendly reminder, the Learn newsgroup (or its Forum interface at=20 https://forum.dlang.org/group/learn) is more appropriate for such questio= ns. Two options come to mind: uint state; enum uint OPEN =3D 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 =3D=3D D)) { func(); } } // Option 2: Equivalent of a C struct struct MaybeExecutor { uint M; Direction D; this (uint M, Direction D) { this.M =3D M; this.D =3D D; } auto opCall(Func)(Func func) { if ((state & M) && (direction =3D=3D D)) { func(); } } } void main() { int* AppsWindow; executeMaybe(OPEN, Direction.OUT, { destroy(*AppsWindow); state &=3D !OPEN; }); auto ex =3D MaybeExecutor(OPEN, Direction.OUT); ex({ destroy(*AppsWindow); state &=3D !OPEN; }); } Ali
Feb 11 2020
next sibling parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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
        }
      )
Feb 11 2020
parent drug <drug2004 bk.ru> writes:
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. Хотя, конечно, на вкус и цвет, товарища нет.
Feb 11 2020
prev sibling parent reply Andrea Fontana <nospam example.com> writes:
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.
Feb 11 2020
next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/11/20 6:58 AM, Andrea Fontana wrote:

 Analog C-code with macros:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 #define TPL(M,D,CODE) if ( state & M &=
& diraction =3D D )
 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ^^^^^^^^^^^^^
 The C code apparently does an assignment inside the macro.
=20
I still think it's a typo. :) Ali
Feb 11 2020
parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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.
Feb 11 2020
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 2/11/20 7:32 AM, =D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 =D0=A4=D0=B0=
=D0=B4=D0=B5=D0=B5=D0=B2 wrote:> On Tuesday, 11 February 2020=20
at 15:08:11 UTC, Ali =C3=87ehreli 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 =3D 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.
I love simple and beautiful more than you do. :) The D code I've shown=20 is virtually identical to the C macro if you rename my 'executeMaybe' to = your 'TPL'. Ali
Feb 11 2020
parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
On Tuesday, 11 February 2020 at 19:42:06 UTC, Ali Çehreli wrote:
 On 2/11/20 7:32 AM, Виталий Фадеев wrote:> 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.
I love simple and beautiful more than you do. :) The D code I've shown is virtually identical to the C macro if you rename my 'executeMaybe' to your 'TPL'. Ali
Yes, Ali. You really love readable & beauty code! Check this one: import std.stdio; enum int INIT = 1; enum int OPEN = 2; enum int CLICK = 3; enum int IN = 1; enum int OUT = 2; class Applications { int state = OPEN; void proc( int message, int direction ) { void TPL( Func )( int M, int D, Func func ) { if ((state & M) && (direction == D)) { func(); } } TPL( INIT, 0, { writeln("INIT"); } ); TPL( CLICK, IN, { writeln("CLICK, IN"); } ); TPL( OPEN, IN, { writeln("OPEN, IN"); } ); TPL( OPEN, OUT, { writeln("OPEN, OUT"); } ); } } void main() { auto apps = new Applications(); apps.proc( OPEN, OUT ); }
Feb 11 2020
prev sibling parent reply =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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 ?
Feb 11 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Feb 11 2020
parent =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= writes:
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!
Feb 11 2020