winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.96k stars 196 forks source link

Remove Map literal syntax, use object literal syntax instead #5700

Open MarkMcCulloh opened 7 months ago

MarkMcCulloh commented 7 months ago

Use Case

Map<t> literals in wing are confusing and not really needed. When you fist consider making something like a map, your first inclination is to:

let x = { a: 2 };

Without a type, this is of course a Json object. This typically isn't much of an issue because of Json's ability to be safely/easily coerced. Like struct literals, these can actually be coerced into Maps.

let x: Map<num> = { a: 2 };
let y: MutMap<num> = { a: 2 };

This works when passing a { ... } object into a function expecting a Map as well. On its own I think this is sufficient reason to remove the verbose { "key" => value } syntax. We could go a step further and align this With Json {} and Struct {} and allow this:

let x = Map<num> { a: 2 };
let y = MutMap<num> { a: 2 };

Proposed Solution

No response

Implementation Notes

No response

Component

No response

Community Notes

Chriscbr commented 7 months ago

I like the idea of simplifying the syntax to use { key: value } for both

yoav-steinberg commented 3 months ago

When implementing this lets make sure punning works too, same as in json:

let a = 2;
let b = 3;
let y = MutMap<num> { a, b, c: 9 };
Chriscbr commented 3 months ago

Let's go forward with this. This change will mean that to create a Map literal you will either need the Map<T> type annotation now (or the object needs to be in a place where that type can be inferred) -- but I think it's worth it to unifying these syntaxes. Later we can add some syntax sugar like:

let x = map { foo: "bar" }; // Map<str>
let y = mutmap { bar: 123 }; // MutMap<num>