-
Notifications
You must be signed in to change notification settings - Fork 108
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
Add support for object literals #119
Comments
We have discussed decorators object literals several times in the past. This proposal attempts to be consistent with decorators for object literals. TC39 has discussed and drafted a PR to tweak object literal semantics to enable this change. Do you see decorators for object literals something that needs to be in the v1 proposal, or would it be OK to handle as a follow-on? |
@littledan It's okay as a follow-on. I'm not personally super invested in this, but you can probably find a few in that es-discuss thread who might. To be fair, the I didn't know it was already being considered by you all, so that's convenient. 🙂 |
I am very interested in object decorators and would like to see them considered soon. Would it increase the complexity of this proposal a lot? |
Object decorators are blocking on this patch. If you want object decorators to move forward, we have to first decide either for or against it. |
afaict, decorators for objects is most useful for concise configuration objects for example: const v = {
@bar('zoom')
foo: getSomeObj()
} it would be nice to be able to decorate by way of properties of objects - where the return value of getSomeObj() gets decorated. |
It could be done using the pipeline operator: const v = {
foo: getSomeObj() |> bar("zoom"),
} |
@nicolo-ribaudo Couldn't most decorator things be done by |
In classes, using a decorator like that you cant modify own fields, change the placement (static/own/prototype) and modify private elements. |
@nicolo-ribaudo well does your example mean something like this: const bar = s => {
return (target) => {
// s is "zoom", target is return value of getSomeObj()
}
}; |
Yes 👍 |
@nicolo-ribaudo can you show an example of using multiple pipeline operator arguments? I assume using decorators for objects literals is more likely than implementing the pipeline operator? I like the pipeline operator b/c it uses horizontal space instead of vertical space. (Decorators would use vertical space). |
Do you mean something like
Well, the pipeline operator is a stage 1 proposal, while a proposal for object decorations doesn't exist yet. |
@nicolo-ribaudo just curious can the pipeline go both directions? getFoo() |> add(2)) <| multiply(3)) |> whatever(3) I assumed it's borrowed from Elixir, but I don't know if it can go both directions in Elixir or not. |
No, the current proposal is only in one direction. We are going out of topic (@littledan maybe you could hide this conversation?). If you want more info you can find them at https://github.com/tc39/proposal-pipeline-operator |
Please please support this feature, I don't want to use singletons just to write nice code. |
I'd like to support decorators on object literals too! Let's do it in a follow-on proposal, as described in https://github.com/tc39/proposal-decorators/blob/master/NEXTBUILTINS.md#object-literals . |
Chiming in to +1 support for object literals, for my purposes it would be to decorate object literals with properties for GUI features like so: const data = {
@UI.ColorPicker()
background: 'blue',
@UI.Range({ min: 0, max: 1, step: 0.01 })
scale: 1
}; This is used in |
Oh, right I forgot about this pattern. This pattern is actively used in other languages. Notably in C# by the Unity engine, which depends on this pattern heavily. |
This is still considered a follow-on proposal; see https://github.com/tc39/proposal-decorators/blob/master/EXTENSIONS.md#object-literal-and-property-decorators-and-annotations |
Any news about it? |
Throwing my +1 on this. Example use case:
import {secret} from '...';
export const defaultConfiguration = () => ({
beds: 5,
cups: 4,
@secret
password: 'CHANGE_ME',
}); ...then later something like... const secrets = somePluginConfiguation[Symbol.Secrets];
Object.entries(secrets).forEach(([key, value]) => {
if ('production' === process.NODE_ENV || whatever) {
return loadSecret(key);
}
else {
return value;
}
}); |
This is still considered out of scope for this proposal, and will potentially be considered for a follow up proposal. |
I'm going to close this issue for the time being given we are not actively seeking to add it to this proposal. |
{
benchmarkIndexList: [],
@observable benchmarkPerformanceList: [],
} Any news about it? |
This has indirectly come up in ES Discuss, in the context of a possible
@lazy
decorator.Where we currently have methods for classes that can be decorated, I think object methods, getters, and setters could be decorated similarly to class instance methods, just with
placement: "own"
(which is obviously never going to be possible for any other type of method). In terms of grammar, you could just reuse the same grammar for class methods.Object properties are deliberately left out, since a function works just as well, and it's easier to write, faster to call, etc.
The text was updated successfully, but these errors were encountered: