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

Support computed properties in Json, Map literals #7157

Open
Chriscbr opened this issue Sep 23, 2024 · 1 comment
Open

Support computed properties in Json, Map literals #7157

Chriscbr opened this issue Sep 23, 2024 · 1 comment
Labels
✨ enhancement New feature or request 📐 language-design Language architecture needs-discussion Further discussion is needed prior to impl

Comments

@Chriscbr
Copy link
Contributor

Use Case

If you are initializing a new Map or Json object and one if its keys is a variable (not a fixed string), then it's not easy to accomplish today. One way to do it is to create a MutJson object, set an item, and then if want it to be immutable, to use Json.deepCopy, but it's awkward and is inefficient because of the copying involved:

let key = "name";
let x = MutJson {};
x[key] = "wingy";
let y = Json.deepCopy(x);

Proposed Solution

Support computed properties, similar to JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names

In Wing this might look like:

let key = "name";
let x = Json {
  [key]: "wingy"
};

An alternative design could be to change Wing's object literal syntax so keys that aren't surrounded by quotes have to refer to variables:

let x = Json {
  key: "wingy" // error: no variable named "key" is defined
};

let key = "name";
let y = Json {
  key: "wingy" // ok
};

But since Wing interoperates with TypeScript, I suspect this would confuse folks and add friction for people switching to Wing, so I recommend against it.

Implementation Notes

No response

Component

Language Design, Compiler

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.
  • If this issue is labeled needs-discussion, it means the spec has not been finalized yet. Please reach out on the #dev channel in the Wing Discord.
@Chriscbr Chriscbr added ✨ enhancement New feature or request 📐 language-design Language architecture needs-discussion Further discussion is needed prior to impl labels Sep 23, 2024
@boyney123
Copy link
Contributor

Personally coming from TS/JS background I prefer this, FWIT

let key = "name";
let x = Json {
  [key]: "wingy"
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request 📐 language-design Language architecture needs-discussion Further discussion is needed prior to impl
Projects
Status: 🆕 New - not properly defined
Development

No branches or pull requests

2 participants