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 for function declarations? #31

Open
rauschma opened this issue Aug 3, 2015 · 3 comments
Open

Decorators for function declarations? #31

rauschma opened this issue Aug 3, 2015 · 3 comments

Comments

@rauschma
Copy link

rauschma commented Aug 3, 2015

Decorators would be very useful for function declarations (incl. generator function declarations). I’d either add that feature to the proposal or mention (e.g. in an FAQ section) why it was postponed/dropped. Python’s decorators work for function declarations.

Use case:

/**
 * Returns a function that, when called,
 * returns a generator object that is immediately
 * ready for input via `next()`
 */
function coroutine(generatorFunction) {
    return function (...args) {
        let generatorObject = generatorFunction(...args);
        generatorObject.next();
        return generatorObject;
    };
}

// Ugly: wrapping
const myCoroutine1 = coroutine(function* () {
    console.log(`First input: ${yield}`);
    return 'DONE';
});

// Nice: decorator
@coroutine
function* myCoroutine2() {
    console.log(`First input: ${yield}`);
    return 'DONE';
}
@sebmck
Copy link

sebmck commented Aug 3, 2015

Previous discussions: #4 and #30.

@rauschma
Copy link
Author

rauschma commented Aug 3, 2015

Thanks! Suggestion: a brief section in the proposal that explains why function declarations can’t be decorated.

@rtm
Copy link

rtm commented Aug 3, 2015

@rauschma Thanks for raising this again. You're absolutely right, the proposal should address this. Hopefully that will highlight the issue so that smart people can figure out how functions can be decorated. The only argument I have seen against decorating function is related to the hoisting issue. However, there is a proposal for dealing with this in the thread on issue #4. Briefly, that proposal is that even if the function is hoisted, the decorator is applied at the point where it is declared. I would like to hear from the proposers why they think this won't work.

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

No branches or pull requests

3 participants