request: python style decorators / aspect orientation

Daniel Keep daniel.keep.lists at gmail.com
Thu May 10 00:36:55 PDT 2007



Chris Nicholson-Sauls wrote:
> Daniel Keep wrote:
>> [...]
>>
>> 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);
> 
> Actually you can leave this check out, as I recall.  Returning values in
> void-return functions is allowed, and I would infer (haven't tested,
> however) that returning a void from a void() would also be fine.  Could
> be wrong.

Hmm; you're right.  I think this is one of those things that doesn't
always work, so I just cut off any problems ahead at the pass.

Actually, this template is something of a re-write of my glSafe template
which does *roughly* the same thing.  Difference there is that I need to
store the return value while I check for errors, and you can't declare a
void variable.

So yeah, more copy+paste than thinking on that one :P

>> }
>>
>> void myFunction_(int param0)
>> {
>>     writefln("Do something with %s...", param0);
>> }
>> alias logFunc!(myFunction_) myFunction;
>>
>> void main()
>> {
>>     myFunction(42);
>> }
>>
> 
> Overall a neat and useful trick.
> 
> -- Chris Nicholson-Sauls

Like I said, I use this sort of thing in GL programming to check for
errors.  So:

glMatrixMode(GL_PROJECTION)

becomes

glSafe!(glMatrixMode)(GL_PROJECTION)

Which automatically throws an exception containing the human-readable
name of the error condition if something goes wrong.  The other neat
side-effect is that I can throw a version switch and have my program log
every GL call made, which makes tracing problems a lot easier.

Tell you what, though; I'd *kill* to be able to do this:

> module mygl;
> static import derelict.gl.gl;
> foreach( symbol ; derelict.gl.gl.symbols )
>     static if( startswith(symbol.stringof, "gl") )
>         mixin(`alias glSafe!(derelict.gl.gl.`~symbol.stringof~`) `
>             ~symbolstringof~`;`);

Which is, incidentally, a trick I used for some Python GL code.  Make my
D code a lot shorter :)

On a side note; is this all Aspect-oriented programming is?  Everything
I read about it basically amounted to "AOP is the Second Coming!!!!!
Also, Java is the best language EVAR!!!!!!" and never really said what
it was or why it was useful.

If all it is is glorified function wrapping, I'm going to be somewhat
disappointed :P

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list