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

Computed Properties #128

Open staycoolcall911 opened 1 year ago

staycoolcall911 commented 1 year ago

The following section should be added to the language reference:

3.9 Computed Properties

You may use the following syntax to define computed properties.
Computed properties are syntactic sugar for getters and setters which themselves are syntactic sugar for methods, therefore omitting either one is acceptable.
"Read block" must always return a value of the same type as the property. Keyword new can be used inside the write block to access the incoming r-value of the assignment (write) operation. new is always the same type as the type of the property itself.
Both preflight and inflight computed properties are allowed.
Keyword var behind computed properties is not allowed. inflight computed properties are also allowed.

// Wing Code:
struct Vec2 {
  x: num;
  y: num;
}

class Rect {
  var size: Vec2;
  var origin: Vec2;

  center: Vec2 {
    read {
      let centerX = origin.x + (size.width / 2);
      let centerY = origin.y + (size.height / 2);
      return Vec2(x: centerX, y: centerY);
    }
    write {
      origin.x = new.x - (size.width / 2);
      origin.y = new.y - (size.height / 2);
    }
  };

  inflight prop: num { /* ... */ }
}
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!

staycoolcall911 commented 1 year ago

Since we replace readwrite with let var, do we want to revise this section of the spec? I think we can also just remove this from the spec for now. @MarkMcCulloh, @yoav-steinberg, @Chriscbr, @hasanaburayyan, @eladb, @ekeren, @ShaiBer - WDYT?

eladb commented 1 year ago

Yes please!

Chriscbr commented 1 year ago

@staycoolcall911 No hard objections from me on removing computed properties to simplify the spec (unless there's another reason?), but just want to say that I think the var modifier and computed properties solve different problems, so I don't think we should confuse them by accident.

staycoolcall911 commented 1 year ago

I completely agree with your last remark @Chriscbr; I was referring to the fact that initially we made computed properties syntax read, write to resemble our former spec for reassignability readwrite. Since we recently removed readwrite, it makes sense to revisit read and write (and I suggest to simplify our spec by removing them altogether, even though they are a completely different feature from readwrite).

eladb commented 1 year ago

Assign this to me. I'll take a look at the spec and make sure it's more consistent with var. I still think we need computed properties, but that's also something we can put as P2