-
Notifications
You must be signed in to change notification settings - Fork 106
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 assignment expressions? const/let declarations? #51
Comments
Use case: runtime guards This came up at es-discuss "Proposal to add symbol: hasInstanceStrict" that
to which TJCrowser replied
The response was that decorators do not allow vetting a value assigned to a local as in const c: PrimitiveNumber = sum(1, 2); This issue came up later on:
|
Propose to use such syntax:
|
I'd prefer to leave it as a follow on. |
I'd like to suggest changing the const decorators extension to include a With a getter on const, you could do lazy evaluation: const @lazy maybeNeeded = () => calc(...args);
if (day === "Sunday") { result += f(maybeNeeded); }
if (month === "June") { result += g(maybeNeeded); } function lazy({ get, set, value }, { kind }) {
let ready = false;
switch (kind) {
case "const": return { get: getLazy, value };
case "let": return { get: getLazy, set: setLazy, value };
default: throw Error("bad kind");
}
function getLazy() {
if (!ready) value = get()(), ready = true;
return value;
}
function setLazy(v) {
set(v);
ready = false;
}
} On another note, can the whole semantics of the I would instead suggest // add +1 when reading
function lazyInc({ get, set }) { return { get: v => get(v) + 1, set }; }
// add +1 on initialization and assignment
function eagerInc({ get, set }) { return { get, set: v => set(v + 1) }; } |
it's very sad that no decorators for functions have appeared yet. Considering that classes themselves are syntactic sugar. They could have been decorators. If you look at the fact that everything in js comes from an object, functions and classes, it becomes funny. I think all frameworks dream that these decorators would be added. For example React, which moved from classes to functions a long time ago and Angular with pipe. And if you look at the fact that everyone is now doing meta-frameworks like next.js, where there is both server and client side. I think these decorators would be indispensable as - for example - middleware. |
Previous discussion: tc39/proposal-decorators-previous#32
OK to leave as a follow-on proposal?
The text was updated successfully, but these errors were encountered: