Code block as template argument

Виталий Фадеев vital.fadeev at gmail.com
Tue Feb 11 20:25:20 UTC 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:
> >> 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 );
}




More information about the Digitalmars-d mailing list