Decorators and Annotations

Dmitry Soshnikov dmitry.soshnikov at
Thu Dec 30 01:52:04 PST 2010


What about to have a bit sugar for AOP in Harmony in respect of
*decorators*or (pre/post)

Python has a common strategy of decorating an annotating methods. E.g.:

def foo(original_fn):
    def decorated_fn(x):
        print("Log", x)
    return decorated fn

def bar(x):
    print("bar: ", x)

Thus, a syntax can be chosen any (since `@` is used in Ruby-style for
"instance variables"). Python uses such decorators not only to wrap some
functions (i.e. `@foo def bar ...` is just a sugar for `bar = foo(bar)`) but
also to change a semantics of some methods -- e.g. `@classmethod` decorator,

Additional info for Pythons decorators:

Also, besides the Python's common decorators, `pre-` and `post-`
`annotations` can be considered.

E.g. (abstract syntax):

[pre checkArguments]
[pre validatePost]
[post formatResponse]

function handleRequest (...args) {
    let data = [...]
    return data

I.e. before entering the `handleRequest` first `checkArguments` and
`validatePost` methods should be called. Then the `handleRequest` itself and
after that `formatResponse`.

Thus, methods-annotations should accept/return a data of a special format.
If e.g. `checkArguments` isn't passed, it should either throw or return a
special `error` data (a signal that other annotation in chain shouldn't
continue). In the successful passing, it just returns handled data which are
passed next to the following annotation/decorator.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list