request: python style decorators / aspect orientation

Nicolai Waniek no.spam at thank.you
Thu May 10 06:06:42 PDT 2007


Daniel Keep wrote:
> 
> This is just a quick hack, but it does work.  The main problem is that
> you have to put the logFunc alias *after* the function is defined, or
> you get forward-reference errors.
> 
> Apart from that, it should do what you want.
> 
> 	-- Daniel
> 
> -----
> 
> module fnwrap;
> 
> import std.stdio;
> import std.traits;
> 
> void logthis(char[] msg)
> {
>     writefln("LOG - %s", msg);
> }
> 
> ReturnType!(typeof(fn)) logFunc(alias
> fn)(ParameterTypeTuple!(typeof(fn)) args)
> {
>     alias ReturnType!(typeof(fn)) returnT;
>     debug
>     {
>         logthis("ENTER - "~(&fn).stringof);
>         scope(exit) logthis("EXIT  - "~(&fn).stringof);
>     }
>     static if( is( returnT == void ) )
>         fn(args);
>     else
>         return fn(args);
> }
> 
> void myFunction_(int param0)
> {
>     writefln("Do something with %s...", param0);
> }
> alias logFunc!(myFunction_) myFunction;
> 
> void main()
> {
>     myFunction(42);
> }
> 



That's really nice, I think I can live with that :-)

best regards



More information about the Digitalmars-d mailing list