request: python style decorators / aspect orientation

Nicolai Waniek no.spam at thank.you
Wed May 9 12:17:29 PDT 2007


Hi everyone,

Hopefully a few of you knew the python style decorators (python's version of
aspect orientation):


def aspectFunc(f):
    def wrapper():
        print "log before function call"
        f()
        print "log after function call"

    return wrapper

@aspecFunc
def myfunction():
    print "hello world"


Would it be possible to have something like this in D? IMHO it would make code
more clear. For example it could look like this when decorating a function:


version (debug) {
    @logFunc
}
void myFunction(int param0)
{
    // do something here
}


instead of:

void myFunction(int param0)
{
    version (debug) {
        logthis("blabla");
    }
    // do something here
    version (debug) {
        logthis("finally we reached an end here");
    }
}

I think it would take all the bloat out of functions that doesn't really belong
to the function. I don't know how much work it would be to implement such a
thing, but I think there would be many cases this could be usefull. If this is
already possible in a sane way, please let me know as this is one of the
features I do like most in python and would like to have in D.

Best regards,
Nicolai



More information about the Digitalmars-d mailing list