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

decorator for variables #32

Open
yongxu opened this issue Aug 4, 2015 · 12 comments
Open

decorator for variables #32

yongxu opened this issue Aug 4, 2015 · 12 comments

Comments

@yongxu
Copy link

yongxu commented Aug 4, 2015

Hey there,

I tried today using babel and realize it doesn't work:

@decorator
let somethingWorkLikeClass = aFunctionThatReturnsObject();

Babel will raise SyntexError: Leading decorator must be attached to a class declaration.

I think it would be very useful to have decorators work on variable, especially when work with some other new features like Object.observe() and decorate class returned by a factory function.

@sebmck
Copy link

sebmck commented Aug 4, 2015

Isn't this ambiguous when you have more than one declarator?

@yongxu
Copy link
Author

yongxu commented Aug 4, 2015

Hey @sebmck,
I couldn't see why it might be ambiguous, could you give an example? Thanks!

@sebmck
Copy link

sebmck commented Aug 4, 2015

@decorator
let somethingWorkLikeClass = aFunctionThatReturnsObject(), foo = "bar";

@yongxu
Copy link
Author

yongxu commented Aug 4, 2015

Thanks @sebmck,
yeah I see the problem now, but I feel it is probably still better to either decorate all the variables or throw an exception just under this special case. I think there are a lot potential using decorator with Object.observe or decorating a returned object.

@loganfsmyth
Copy link

@yongxu What does this provide that is not covered by doing

let somethingWorkLikeClass = decorator(aFunctionThatReturnsObject());

though?

@yongxu
Copy link
Author

yongxu commented Aug 4, 2015

@loganfsmyth Yes, that is actually what I did when I found out decorator doesn't work on variables.
I just think it would be great to apply decorator on variables.
I think decorator will make code much cleaner if we use if for higher order components, or distinguish difference between use a variable as function input and extend a variable to make it more powerful(such as polyfill and wrapper)

@nevir
Copy link

nevir commented Oct 15, 2015

Allowing decorators on assignment (and/or export) expressions would certainly help for more complicated decoration, while still being easy to read:

@memoize({ttl: 30})
export function getStuff(id) {
  ...
}

Instead of:

export const getStuff = memoize({ttl: 30})(function getStuff(id) {
  ...
});

@jkrems
Copy link

jkrems commented Jan 20, 2016

There's also the unfortunate "function naming" thing. The only way to have named functions with "decorators" is the following right now:

export const getStuff = memoize({ ttl: 30 })(function getStuff(id) {});

Compared to (given fn.name semantics for assignments):

@memoize({ ttl: 30 })
export const getStuff = () => {}

@D1plo1d
Copy link

D1plo1d commented Jan 28, 2016

Yes the @memoize syntax is much more readable for me as well. The readability problem of the current syntax compounds when you try to use multiple decorators.

Class decorators have been used by the React community extensively in the form of so-called Higher Order Components. Personally I have found Higher Order Components to promote a simpler coding style then mixins with more obvious boundaries between objects. I sincerely hope we can bring a syntax to encourage this coding style back into es7 through export decorators like @jkrems example.

@sap9433
Copy link

sap9433 commented Feb 16, 2016

Another use case . With react stateless component decorator doesn't work . same issue Leading decorators must be attached to a class declaration

@connect((state) => {
        return {
            someVAr: state.someStateProp
        };
    }
)
const Layer = (props) => {
  return <div > hi  {this.props.someVAr}< /div>);
};

@silkentrance
Copy link

I think that this is pretty much the same as for free function decorators. Hoisting might be a problem here.

And, in accordance to your overall stance towards free function decorators, this should be a won't fix and should be closed (for future review of course, once we get rid of that annoying hoisting feature) in order to get this going again.

@Hypnosphi
Copy link

Hoisting might be a problem here

let and const aren't hoisted

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

9 participants