Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators #996

Closed
DmitrySoshnikov opened this issue Dec 30, 2010 · 8 comments
Closed

Decorators #996

DmitrySoshnikov opened this issue Dec 30, 2010 · 8 comments

Comments

@DmitrySoshnikov
Copy link

Unfortunately, the search by issues is currently broken, so cannot check whether such a feature was already proposed.

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

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

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

@foo
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, etc.

Additional info for Pythons decorators: http://www.python.org/dev/peps/pep-0318/

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

E.g. (abstract syntax):

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

handleRequest = (...) ->
    data = [...]
    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.

Dmitry.

@StanAngeloff
Copy link
Contributor

Try here http://githubissues.heroku.com/#jashkenas/coffee-script/ and switch to Closed before searching for decorator and annotation.

@DmitrySoshnikov
Copy link
Author

Yep, thanks, found -- a very old issue (enchantment, wontfix): https://github.com/jashkenas/coffee-script/issues/issue/76

Yeah, a (simple/extended) wrap util is about the case. The thing was in exactly avoiding wrapping and assigning back to the same identifier.

I.e. foo = wrap(foo, bar) vs. @bar foo. Although, of course a very common case of a wrapper is useful and can (and could always) be implemented in the language itself without sugar.

So, if it was decided so long time ago, I think this ticket can be closed also.

Dmitry.

@michaelficarra
Copy link
Collaborator

Here's a simple wrap for you:

unless Function::wrap? then Function::wrap = (fn,bind) ->
    self = @
    (args...) ->
        fn.call @, (if bind? then self.bind bind else self), args

And here's how you can use it:

someFunction = -> print "second"
wrapped = someFunction.wrap (original,args) ->
    print "first"
    original args...
    print "third"
wrapped()

Or, something a little more advanced:

Function::curry = (curriedArgs...) ->
    @wrap (original,passedArgs) ->
        original.apply @, curriedArgs.concat(passedArgs)

@DmitrySoshnikov
Copy link
Author

Yes, as already was mentioned, a wrap util can (and always could before) be implemented in the language itself. The talk was only about a syntactic sugar for it.

@factoidforrest
Copy link

Well this is basically the end of coffeescript if they don't start supporting modern features like this.

@vendethiel
Copy link
Collaborator

vendethiel commented Mar 10, 2017

Death rumors again?

@vendethiel vendethiel reopened this Mar 10, 2017
@LoveIsGrief
Copy link

LoveIsGrief commented Mar 11, 2017

Hello,

Passing by from codetriage.com and trying to help sort (trier?) issues.

Since this is an ES6 proposal as well, it soon might not be considered an extension anymore. Here's an article that seems to imply decorators are already possible in ES6 Only problem I see is choice of a new character for denote a decorate, because @ has already been taken.

Since this was reopened maybe adding the enhancement tag would help?

(If I'm doing this triage wrong, do tell. I'm here to learn :) )

@vendethiel
Copy link
Collaborator

Since this was reopened maybe adding the enhancement tag would help?

It wasn't reopened on purpose. It was just closed so long ago, posting a comment removed the "closed" tag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants