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

can't call `super` method from inflight closure that's inside a class #3474

Open yoav-steinberg opened 11 months ago

yoav-steinberg commented 11 months ago

I tried this:

class BaseClass {
  pub inflight m1(): str {
      return "base inflight m1";
  }
}

bring cloud;
let q = new cloud.Queue();
class ExtendedClass extends BaseClass {
  pub inflight m1(): str {
    return "extended inflight m1";
  }
  pub get_func(): cloud.Function {
    let inflight_closure = inflight (s:str): str => {
      return "this: ${this.m1()}, super: ${super.m1()}";
    };
    return new cloud.Function(inflight_closure);
  }
}
let y = new ExtendedClass().get_func();
test "inflight closure accesses super" {
   assert(y.invoke("") == "this: extended inflight m1, super: base inflight m1");
}

This happened:

error: `super` calls inside inflight closures not supported yet, see: https://github.com/winglang/wing/issues/3474
   --> wing/main.w:15:50
   |
15 |       return "this: ${this.m1()}, super: ${super.m1()}";
   |                                                  ^^ `super` calls inside inflight closures not supported yet, see: https://github.com/winglang/wing/issues/3474

I expected this:

To run and pass the assertion

Is there a workaround?

have another method in the base class with the same code and call it through this.

Component

Compiler

Wing Version

No response

Node.js Version

No response

Platform(s)

No response

Anything else?

The difficulty in resolving this is that we need to create a similar mechanism to the __parent_this mechanism. Just that it'll be a "super" object and not a "this" object. Alternatively we can find a way to "elevate" the __parent_this to "reference" the super instance. Both these things require JS trickery using prototype chain magic and perhaps Proxy objects.

It might be worth considering if the inflight closure transformation is still required now that we more-or-less support free var (global var) lifting.

Community Notes

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

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

github-actions[bot] commented 4 months 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!

github-actions[bot] commented 4 weeks 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!