Skip to content

Decorators for function declarations? #31

@rauschma

Description

@rauschma

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';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions