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.78k stars 189 forks source link

Immutability in user-defined types #1369

Open eladb opened 1 year ago

eladb commented 1 year ago

Community Note

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.

Feature Spec

It is now possible to somehow tell the Wing compiler that user-defined classes can be captured safely.

This means we need some explicit immutability shit.

Use Cases

Be able to capture user-defined types

Implementation Notes

No response

Component

No response

Chriscbr commented 1 year ago

explicit immutability shit

Related: https://github.com/winglang/wing/issues/64

We would need some mechanism to determine whether something like this is type-safe (inflight-safe) or not:

bring "my-restaurant" as my_restaurant;

let order = new my_restaurant.Order();
orders.addFood("hummus");

new cloud.Function(inflight (m: str): str => {
  print(order.to_str());
});

orders.addFood("pita"); // mutation bad! 👻
eladb commented 1 year ago

👍

ShaiBer commented 1 year ago

@Chriscbr in your example it is not even telling the compiler that order is immutable, it is telling it that it is immutable from a certain point, which makes me think if we don't want to add this mechanism for language structures as well (for instance Set). If the answer is no for language structures then I think it should also be no for other objects. In this case, assuming we add a freeze keyword, your example will look something like this:

bring "my-restaurant" as my_restaurant;

let order = new my_restaurant.Order();
order.addFood("hummus");

let changed_order = freeze deepClone(order);

new cloud.Function(inflight (m: str): str => {
  print(changed_order.to_str());
});

order.addFood("pita"); // mutation allowed :)
changed_order.addFood("pita"); // mutation bad! 👻
Chriscbr commented 1 year ago

A freeze keyword is a neat idea

github-actions[bot] commented 1 year 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!

eladb commented 1 year ago

Keep

github-actions[bot] commented 1 year 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!

github-actions[bot] commented 1 month ago

Hi,

This issue hasn't seen activity in 90 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!