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);
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.
Use Case
If you are initializing a new
Map
orJson
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 aMutJson
object, set an item, and then if want it to be immutable, to useJson.deepCopy
, but it's awkward and is inefficient because of the copying involved: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:
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:
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