Pure Aspect-Oriented Program: an example

Hung Jung Lu hungjunglu at yahoo.com
Sun Nov 9 05:26:54 EST 2003


hungjunglu at yahoo.com (Hung Jung Lu) wrote in message 
> Here I only present a possible syntax of the "around" advice.
> ...
> aspect A {
>     method f_before_A = f
>     method f(...) {
>         print 'begin around-advice'
>         result = this.f_before_A()
>         print 'end around-advice'
>         return result
>     }
> }

(I changed comp.lang.java to comp.lang.java.programmer)

Yikes, that's awfully bad. It violates functional programming
philosophy of the meta-methods.

I finally figured out a cleaner syntax for the "around" advice, using
@-decorated names and method hooks.

class M {
    method f(x) {
        print 'multiply by 2'
        result = 2 * x
        print 'result =', result
        return result
    }
}

aspect A {
    codeblock f_with_A {
        print 'post-multiply by 3'
        result = 3 * &f(...)
        print 'result =', result
        return result
    }
    method f at A(...) {
        this.f_with_A {
            &f = this.f@
        }
    }
}

aspect B {
    codeblock f_with_B {
        print 'pre-multiply by 4'
        x = 4 * x
        result = &f(...)
        print 'result =', result
        return result
    }
    method f at B(...) {
        this.f_with_B {
            &f = this.f@
        }
    }
}

aspect C inherits A, B {
    method f at C(...) {
        this.f_with_B {
            &f = this.f at A
        }
    }
}

endow M with C
m = new M()
x = 1
print 'input =', x
print m.f(x)

//------ output
input = 1
pre-multiply by 4
post-multiply by 3
multiply by 2
result = 8
result = 8
result = 24

Notice that I have used a method hook &f, not a codeblock hook. Notice
also the usage of (...) for signature decoupling.

Aspect C will create the "@-decorated" methods f at A, f at B and f at C for
class M. The original method can always be accessed as f@(x). When
f(x) is accessed without name decoration, the latest @-decorated
implemention is used. However, in order to avoid meta-method
ambuiguities in multiple-aspect inheritance, the hooking process
should always be done with explicit @-decorated names. If you really
don't like a particular meta-method, you can always override it. So I
think usage of explicit @-decorated name inside a meta-method should
be OK.

I guess the @-decoration could be used in all meta-methods, that is,
even for the cases of "before" and "after" advices.

Hung Jung




More information about the Python-list mailing list