digitalmars.D - Code block as template argument
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (41/41) Feb 11 2020 I want template this code
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (43/84) Feb 11 2020 Version 2 of question. more accurate.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (45/55) Feb 11 2020 On 2/11/20 3:14 AM, =D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 =D0=A4=D0...
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (4/16) Feb 11 2020 Thank!
- drug (35/36) Feb 11 2020 How the following:
- Andrea Fontana (2/16) Feb 11 2020 The C code apparently does an assignment inside the macro.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/13) Feb 11 2020 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0...
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (3/12) Feb 11 2020 Thank. You understand me rigth, but your presented code too big.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (7/22) Feb 11 2020 On 2/11/20 7:32 AM, =D0=92=D0=B8=D1=82=D0=B0=D0=BB=D0=B8=D0=B9 =D0=A4=D0...
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (38/63) Feb 11 2020 Yes, Ali. You really love readable & beauty code!
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (17/38) Feb 11 2020 Wow! Thank! I mass.
- Steven Schveighoffer (18/57) Feb 11 2020 D does not have macros. Instead we have mixins. mixins allow you to
- =?UTF-8?B?0JLQuNGC0LDQu9C40Lkg0KTQsNC0?= =?UTF-8?B?0LXQtdCy?= (3/22) Feb 11 2020 Steve, it cool! Thank!
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
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
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
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:Thank! We love small beauty code. Smaller is better. Readable is better. Like this:[...]CODE;[...][...]TPL( OPEN, OUT, { delete AppsWindow state &= !OPEN } )
Feb 11 2020
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
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:The C code apparently does an assignment inside the macro.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
Feb 11 2020
On 2/11/20 6:58 AM, Andrea Fontana wrote:& diraction =3D D )Analog C-code with macros: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 #define TPL(M,D,CODE) if ( state & M &==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. =20I still think it's a typo. :) Ali
Feb 11 2020
On Tuesday, 11 February 2020 at 15:08:11 UTC, Ali Çehreli wrote:On 2/11/20 6:58 AM, Andrea Fontana wrote:Thank. You understand me rigth, but your presented code too big. We love simple, beauty.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
Feb 11 2020
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: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'. AliOn 2/11/20 6:58 AM, Andrea Fontana wrote:Thank. You understand me rigth, but your presented code too big. We love simple, beauty.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
Feb 11 2020
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: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 ); }D )On 2/11/20 6:58 AM, Andrea Fontana wrote:Analog C-code with macros: #define TPL(M,D,CODE) if ( state & M && diraction =^^^^^^^^^^^^^big.Thank. You understand me rigth, but your presented code tooThe C code apparently does an assignment inside the macro.I still think it's a typo. :) AliWe 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
Feb 11 2020
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: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 ?On 2/11/20 3:14 AM, Виталий Фадеев wrote:> On Tuesday, 11 February 2020 at 11:10:44 UTC, Виталий Фадеев wrote:The C code apparently does an assignment inside the macro.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
Feb 11 2020
On 2/11/20 10:30 AM, Виталий Фадеев wrote:On Tuesday, 11 February 2020 at 14:58:58 UTC, Andrea Fontana 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; }); -SteveOn Tuesday, 11 February 2020 at 11:34:49 UTC, Ali Çehreli wrote: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 ?On 2/11/20 3:14 AM, Виталий Фадеев wrote:> On Tuesday, 11 February 2020 at 11:10:44 UTC, Виталий Фадеев wrote:The C code apparently does an assignment inside the macro.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
Feb 11 2020
On Tuesday, 11 February 2020 at 15:51:31 UTC, Steven Schveighoffer wrote:On 2/11/20 10:30 AM, Виталий Фадеев wrote:Steve, it cool! Thank![...]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