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.81k stars 191 forks source link

Struct mutability #5268

Open Chriscbr opened 7 months ago

Chriscbr commented 7 months ago

I tried this:

Reassigning a field of a struct

bring cloud;

struct Person {
  name: str;
  age: num;
}

let person = Person { name: "Alice", age: 30 };
person.age += 1;

This happened:

error: Variable is not reassignable
   --> main.w:10:3
   |
 5 |   age: num;
   |   --- defined here (try adding "var" in front)
   .
10 |   person.age += 1;
   |   ^^^^^^^^^^ Variable is not reassignable

I expected this:

The error says to try adding "var" in front, but the language doesn't currently allow individual struct fields to be labeled "var", so the error needs to be fixed.

Is there a workaround?

No response

Anything else?

No response

Wing Version

0.53.0

Node.js Version

20.9.0

Platform(s)

MacOS

Community Notes

Chriscbr commented 7 months ago

Maybe we can see whether it's possible to reassign to struct fields without introducing holes in the language's mutability model, or we should change the error message to "Struct fields are not reassignable" for now.

eladb commented 7 months ago

Perhaps we should rename this issue to "struct mutablity" instead of "struct reassignablity".

We discussed the idea of adding a mut keyword to denote that a type is mutable, so instead of: MutArray or MutMap you would use mut Array and mut Map.

Then, I would use mut for structs like so:

struct Foo { name: str };

let foo = mut Foo { name: "hello" };
foo.name = "bang";
github-actions[bot] commented 5 months ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!