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 generators? #24

Open
dead-claudia opened this issue Jun 19, 2015 · 6 comments
Open

Decorators for generators? #24

dead-claudia opened this issue Jun 19, 2015 · 6 comments

Comments

@dead-claudia
Copy link

This is the one part I was surprised didn't exist. Every other possibility I can think of is covered with this proposal, but I think generators might have been overlooked.

I have already come across a problem where I needed this functionality, and I ran into issues.

Is there any reason generators shouldn't be decorated?

@dead-claudia
Copy link
Author

FWIW, here's my use case:

function checkCallable(desc) {
    const original = desc.value
    desc.value = function (f) {
        assertCallable(f, 'f')
        return original.apply(this, arguments)
    }
}

function wrap(desc) {
    const original = desc.value
    desc.value = function () {
        return new Wrapper(original.apply(this, arguments))
    }
}

class Wrapper {
    // ...

    @checkCallable @wrap *map(f) {
        let index = 0
        for (const entry of this) {
            yield f(entry, index++)
        }
    }

    @checkCallable @wrap *filter(f) {
        let index = 0
        for (const entry of self) {
            if (f(entry, index++)) {
                yield entry
            }
        }
    }

    // ...
}

@rosshadden
Copy link

👍 I'm surprised this didn't work already, as generators seem to work just like normal functions for everything else (like static class methods, for example).

@optikalefx
Copy link

We're making an app using KOA, and it was exciting to use decorators to give all of our methods an @verb and and @param. But it seems generator functions don't accept decorators yet. +1 for this feature.

@galenwarren
Copy link

Running into the same issue, trying to decorate generator functions in a Koa app for routing purposes. From another thread, I see this is an issue of ambiguous grammar, but that thread seems to suggest the underlying issue is resolved: https://phabricator.babeljs.io/T2014. Is there a way to get this to work now?

@dead-claudia
Copy link
Author

@galenwarren You are correct (#34). I think the only outstanding part remaining is a PR.

@silkentrance
Copy link

Actually this should work and was never limited by the spec, but it does not due to a "bug" in babylon. See also #66.

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

5 participants