winglang / wing

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

Unable to pass methods (static or instance) as closures #3250

Open azjgard opened 1 year ago

azjgard commented 1 year ago

I tried this:

Given this starter code:

bring cloud;

let api = new cloud.Api();

api.get("/", inflight (request: cloud.ApiRequest): cloud.ApiResponse => {
  return cloud.ApiResponse {
    status: 200,
    body: "Hello, world!"
  };
});

I moved the controller logic into a static method of a class:

inflight class RequestController {
  static inflight handleRequest(request: cloud.ApiRequest): cloud.ApiResponse {
    return cloud.ApiResponse {
      status: 200,
      body: "Hello, world!"
    };
  }
}

And then tried to use that static method to handle requests:

api.get("/", RequestController.handleRequest);

This happened:

The following error was thrown during compilation:

ERROR: Cannot read properties of undefined (reading 'node')

target/gdpr.wsim.233847.tmp/.wing/preflight.js:44
       }
       const api = this.node.root.newAbstract("@winglang/sdk.cloud.Api",this,"cloud.Api");
>>     (api.get("/",RequestController.handleRequest));
     }
   }

I expected this:

I expected the console to compile and requests to be handled successfully.

Is there a workaround?

Don't use class methods for handling API requests, and define route controllers using anonymous inflight functions (like in the original code sample).

Component

Compiler

Wing Version

0.23.17

Wing Console Version

No response

Node.js Version

v20.3.1

Platform(s)

MacOS

Anything else?

Community Notes

ekeren commented 1 year ago

updated description and generated two bugs

staycoolcall911 commented 1 year ago

The original issue showed 2 different issues #3251, #3252. After #3252 was closed as a duplicate of #457, we decided to close #3251 and keep this issue. I revised the issue's title and description to reflect all that.

eladb commented 1 year ago

Updated title

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

Checked it now and the current status is quite the same:

Ran this:

bring cloud;

let api = new cloud.Api();

inflight class RequestController {
  pub static inflight handleRequest(request: cloud.ApiRequest): cloud.ApiResponse {
    return cloud.ApiResponse {
      status: 200,
      body: "Hello, world!"
    };
  }
}

api.get("/", RequestController.handleRequest);

Received this error:

error: Cannot reference an inflight value from within a preflight expression
   --> bla.w:14:14
   |
14 | api.get("/", RequestController.handleRequest);
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot reference an inflight value from within a preflight expression
github-actions[bot] commented 11 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 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 2 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!