February 11, 2020 Re: Code block as template argument | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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!
|
February 11, 2020 Re: Code block as template argument | ||||
---|---|---|---|---|
| ||||
Posted in reply to Виталий Фадеев | 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 |
February 11, 2020 Re: Code block as template argument | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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 );
}
|
Copyright © 1999-2021 by the D Language Foundation