-
Notifications
You must be signed in to change notification settings - Fork 126
Open
Description
Decorators for classes, methods, and object properties are a really nice extension to the language. The only extra one I would like to see is the ability to add decorators to function declarations:
@RunOnce
function expensiveOperation() {
return 1 + 1;
}
de-sugars to:
var expensiveOperation = RunOnce(function expensiveOperation() {
return 1 + 1;
});
This suggests allowing decorators for any assignment, but I think that might be a bit much so I'm leaving it out of this PR:
// For the curious, that would allow constructs like this
@RunOnce
let expensiveOp = () => 1 + 1
// De-sugars to
let expensiveOp = RunOnce(() => 1 + 1);
nickell, rickmed, timhwang21, darsain, btipling and 125 moreiamthesiz, davidqqq, stylemistake, matthewhuang-camelot, laurentpayot and 26 morelaurentpayot, liron-navon, mprinc, rogerpadilla, Heartnett and 7 morelaurentpayot, mprinc, acro5piano, rogerpadilla, Heartnett and 5 more
Activity
sebmck commentedon Mar 19, 2015
The issue with adding decorators to function declarations is that they're hoisted which means hoisting the decorators too which changes the position in which they're evaluated.
svieira commentedon Mar 19, 2015
True, and that is a potential foot-gun, but I cannot think of any cases where source position is likely to be needed at run-time (I can see using decorators as targets for compile-time transformations using sweet-js, but in those cases the function hoisting behavior does not apply.) As I understand it, hoisting is likely to (though not specified to) happen in order, so even if you had a stateful decorator that added each decorated function to a queue, the functions would still run in order.
Am I just being dense and missing an obvious use case where source position could make or break functionality?
sebmck commentedon Mar 19, 2015
Some examples:
The behaviour is non-obvious and it's impossible to sprinkle to get obvious behaviour. It's similar to class declarations not being hoisted because their extends clause can be an arbitrary expression.
svieira commentedon Mar 19, 2015
Chuckles Fair enough - thanks for taking the time to point out the issues!
nmn commentedon Apr 9, 2015
The noted problems here suggest to me that decorators are possibly going to have very much the same problem as operator overloading is supposed to have.
The major concern I have with decorators is people writing too many decorators with not very good names. Or even for things that are pretty complicated. The concern I have is that decorators are only syntactic sugar to make code more declarative. But I hope they won't end up creating code that is often misleading and full of surprises.
I personally don't think any feature's merits should be judged by considering the worse use-cases. But I think it's still interesting to think about them to remove as many footguns as possible.
Relevant Point: Perhaps, functions decorators SHOULD work for functions. But since hoisting is the problem, Hoisting could be disabled for any function that is decorated. Since Function assignments work the same way, I don't think its too confusing.
sebmck commentedon Apr 9, 2015
@nmn
It is confusing. Function declarations that are hoisted sometimes isn't going to cut it.
nmn commentedon Apr 9, 2015
@sebmck Fair point.
Thinking about it more, perhaps they are no so important for functions.
Functions that take take and return functions are pretty easy to write. They are more useful in classes as the syntax keeps things from getting hairy.
Thanks.
Ciantic commentedon Apr 17, 2015
Why are they not important for functions? Memoize is just as needed in functions as well as in classes. Also for currying, who doesn't curry their functions ;)
If problem is you can accidentally use class decorators in functions, maybe another syntax:
double at, or something.
Edit: Is hoisting really a problem? Being used Python a lot which implements function decorator, I don't think the order of decoration is used to anything except in bad code, which is really hard to safeguard anyway.
sebmck commentedon Apr 17, 2015
@spleen387 Hoisting is still a problem.
nmn commentedon Apr 17, 2015
first a question: do generator functions get hoisted? If not isn't that confusing as well.
If yes, there is already a proposal for making custom wrappers around generators, like async functions.
How does this proposal deal with hoisting?
nmn commentedon Apr 17, 2015
Another approach, let the function get hoisted. But the decorator should be applied where it is defined.
So this:
will desugar (and hoist) to:
sebmck commentedon Apr 17, 2015
Generator function declarations are definently hoisted. Hoisting the
function declaration and not the decorator is unintuitive and confusing.
You can just wrap the function in a method call and it expresses the exact
same thing without the confusing semantics.
On Friday, 17 April 2015, Naman Goel notifications@github.com wrote:
Sebastian McKenzie
nmn commentedon Apr 17, 2015
@sebmck I just updated my first comment. How does compositional functions deal with hoisting?
You can just wrap the function in a method call and it expresses the exact same thing without the confusing semantics.
I get that, but the same could be said about classes as well.
I don't like the idea of decorators for only classes, and I'm just trying to find a solution to the hoisting problem. I feel like having decorators for only classes makes the language more disjointed and inconsistent than it already is.
51 remaining items
ackvf commentedon Mar 13, 2018
const
andlet
behave slightly different. Cannot we have decorators for these?ukari commentedon Mar 13, 2018
@ackvf
identifier claimed by
const
can't be reassign, so the snippet you give can't be translate intoreassign-style
instead of
reassign-style
, it could be translate intowrap-style
but
reassgin-style
has a benefit that help foo get identifier's name when it assign to a function, like thisI implement let decorator in babel, here is a example which needs the feature to get let function's name.
javascript-let-decorators
Hypnosphi commentedon May 4, 2018
You might want to use pipeline operator for that instead:
stylemistake commentedon Jul 2, 2018
Why do we have a problem with removing hoisting altogether? Classes don't hoist, const/let declarations don't hoist as well. I suggest that at the moment user decorates a function, it becomes unhoisted, and it's his responsibility to properly position that function within the code, just like with classes.
For example this:
...gets transformed into this:
And if user assumes that it's hoisted, throw a ReferenceError:
I believe JS should softly guide people away from hoisting, and remove bad language features overall.
mlanza commentedon Jul 5, 2018
It'd be a shame to exclude decorators from plain functions.
It was said that hoisting isn't going away, one can assume backwards compatibility. But what if something akin to "use strict" – e.g. "no hoisting" – could be added to the top of a script/module? It would be considered a compilation hint. You would only use it in situations like this where hoisting gets in the way.
Macil commentedon Jul 5, 2018
My understanding is that the big reason a script-wide feature like strict mode actually made it through in the first place was not because it threw away old misfeatures, but because the act of throwing away those misfeatures enabled brand new valuable use cases (SES / Google Caja javascript sandboxing). And the separate modes idea finally went away in the new happy-path case: modules force strict mode on. I think the standards groups would be extremely hesitant to reverse that victory to re-add a new script/module-wide "use stricter" mode.
Instead of script/module-wide settings, I think it makes a lot of sense to look at how the async-await feature added a new "await" keyword. "await" was not a reserved word in Javascript prior to async-await being added, so if it were added as a new reserved word, then all code that used "await" as a variable name would break. The standard solved this by making "await" a keyword only inside of async functions. The standard avoided breaking old code by making you opt in to the new behavior by making a function async.
Making it so decorated functions don't hoist seems to me like a strategy consistent with how async-await was added without breaking old code. You locally opt in to the new behavior by using a new feature.
tlrobinson commentedon Mar 7, 2019
I don't like being that +1 guy, but this is sorely missed. Assignment decorators (tc39/proposal-decorators#51) and disabling hoisting on decorated function declarations seem like very reasonable solutions.
dimaqq commentedon May 30, 2019
Come from the fair land of Python, [plain / const x = () =>] function decorators are perhaps more important than class member function decorators.
finom commentedon Aug 29, 2019
Decorators for functions allow to define a custom how a function should behave. We already have similar and a "hardcoded" syntax of async functions:
(yep, there is also
await
syntax but it doesn't matter at this case)Why not to allow to do that for custom function modifications:
A more specific example (the thing I was trying to implement before I discovered that there is no decorators for functions) is a multi-thread calculation via inline Worker:
And currently I need to wrap the
myWorker
function like this:or worse:
which is very inobvious if a function gets more than a few lines.
This is a really wanted feature. I hope somebody more experienced than me could make a spec for that.
More examples (also borrowed from above messages):
Some syntax examples:
finom commentedon Sep 4, 2019
Guys, I've made a little article about the function decorators: https://github.com/finom/function-decorators-proposal and also started a discussion at ESDiscuss mailing list. I don't know where it leads but I hope to get at least a little chance of approval of stage 0 from TC39 committee.
TL;DR
JulianLang commentedon Feb 10, 2020
Actually this reality today, as this is what one have to do in Angular for Pipes:
see: https://angular.io/guide/pipes#custom-pipes