Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to capture a variable that is an inflight closure #2236

Closed
eladb opened this issue Apr 24, 2023 · 3 comments · Fixed by #2580
Closed

Unable to capture a variable that is an inflight closure #2236

eladb opened this issue Apr 24, 2023 · 3 comments · Fixed by #2580
Assignees
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler

Comments

@eladb
Copy link
Contributor

eladb commented Apr 24, 2023

I tried this

bring cloud;

resource MyResource {
  closure: inflight (str): str;

  init() {
    this.closure = inflight (s: str): str => { return "hello"; };
  }

  inflight foo() {
    this.closure("hello");
  }
}

I expected this:

To be possible

Instead, this happened

error: Cannot capture field 'closure' with non-capturable type 'inflight (str): str'
   --> .[/test.w:11:5](vscode://file//test.w:11:5)
   |
11 |     this.closure("hello");
   |     ^^^^^^^^^^^^ Cannot capture field 'closure' with non-capturable type 'inflight (str): str'

Is there a workaround?

No response

Component

Compiler

Wing Version

No response

Wing Console Version

No response

Node.js Version

No response

Platform(s)

No response

Anything else?

No response

Community Notes

  • 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.
@eladb eladb added the 🐛 bug Something isn't working label Apr 24, 2023
@Chriscbr Chriscbr added the 🛠️ compiler Compiler label Apr 24, 2023
@Chriscbr
Copy link
Contributor

I agree it makes sense to support this, but I think there are workarounds available (for example, using cloud.Function instead of an inflight closure).

#1448 should fix this

@Chriscbr
Copy link
Contributor

This produces a new error on Wing 0.17.7, but it's the same error as #2236

error: Expected type to be "inflight (str): str", but got "$Inflight1" instead
  --> capture_closure.w:7:20
  |
7 |     this.closure = inflight (s: str): str => { return "hello"; };
  |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Expected type to be "inflight (str): str", but got "$Inflight1" instead

@mergify mergify bot closed this as completed in #2580 May 22, 2023
mergify bot pushed a commit that referenced this issue May 22, 2023
Fixes a limitation that prevented users from calling inflight functions that were defined in preflight. The main thrust of the strategy is to leverage the fact that inflight functions defined in preflight are compiled into classes with a single inflight method named "handle" (#2411) - we can call these "handler classes".

To allow for the possibility that values typed as inflight functions could refer to handler classes OR to plain inflight functions (that were declared in an inflight scope, so they didn't need to get converted into a handler class), we desugar inflight call expressions into `typeof <fn> === "function" ? await <fn>() : await <fn>.handle()` to support either case.

Fixes #390
Fixes #2236
Fixes #2242

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Monada Contribution License](https://docs.winglang.io/terms-and-policies/contribution-license.html)*.
@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.17.22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants