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

Proposal: implicit scope for any function #287

Open
eladb opened this issue Oct 18, 2022 · 10 comments
Open

Proposal: implicit scope for any function #287

eladb opened this issue Oct 18, 2022 · 10 comments
Labels
📐 language-design Language architecture

Comments

@eladb
Copy link
Contributor

eladb commented Oct 18, 2022

I am wondering if the notion of implicit scopes, which we already have for resource initializers, can be extended to methods/functions as well.

So basically any method/functions/constructor that has a first argument called scope of type Construct is called without explicitly specifying the scope, and can be customised with "in SCOPE" syntax of course.

This comes up quite a lot, especially in static factory methods. It means that any code that runs in wing always have access to the parent scope that called it. Something that we struggled with in the CDK quite a lot.

The follow up question is what happens in Wing code. If I want to access scope from any method/function, does it mean the compiler will implicitly include an additional first parameter to the function signature. That's a breaking change...

Perhaps all wing functions should always have an implicit scope as the first argument? Don't kill me!

@eladb eladb added the 📐 language-design Language architecture label Oct 18, 2022
@MarkMcCulloh
Copy link
Contributor

Given our current design, I think we always assume any preflight code is running in some scope so the thought makes sense.
Why not go all-in and make it a keyword (ala this) without relying on an arg?

@eladb
Copy link
Contributor Author

eladb commented Oct 19, 2022

Why not go all-in and make it a keyword (ala this) without relying on an arg?

Yes, totally think we need a keyword (scope), but we also need a way to actually propagate this through the JS code. I think an implicit argument passed to all functions can do the trick. Perhaps in JS code generated by Wing we can pass it in always as the last argument of every function call and name it $scope (but this is incompatible with how construct initializers look like, so this means there's a special case there).

@ShaiBer
Copy link
Contributor

ShaiBer commented Oct 20, 2022

Maybe we can use JS bind, it takes the "this" arg, but also others. So we can explicitly set the this param and add a scope param to all functions without changing their signatures

@github-actions
Copy link

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 github-actions bot added the Stale label Dec 20, 2022
@eladb
Copy link
Contributor Author

eladb commented Dec 20, 2022

Keep please

@github-actions github-actions bot removed the Stale label Dec 21, 2022
@Chriscbr
Copy link
Contributor

A possible use case for this would be for the SDK's Schedule/Timer/Cron resource:

let timer = cloud.Timer.fromCron("* * * * 6"); // implicit scope and id
timer.on_tick(inflight () => {
  // ...
});

@eladb
Copy link
Contributor Author

eladb commented Jan 10, 2023

yes, I think the "factory" use case is strong. I was thinking that we need to give factories a more first-class status in the language. In a sense an initializer and a static factory method are sort of the same thing.

@Chriscbr
Copy link
Contributor

Chriscbr commented Feb 6, 2023

Here's a random idea. What if whenever a function in a JSII/Wing library (instance method, static method, constructor, etc.) has its first two arguments named "scope" and "id", then the Wing compiler will automatically remove them from the signature and automatically fill in the scope and id during jsification?

(I think this would need to be fleshed out because of edge cases, e.g. does this apply to both classes and resources? Are there any places in Wing where there isn't a valid implicit scope?)

@eladb
Copy link
Contributor Author

eladb commented Feb 7, 2023

@Chriscbr yes I like this a lot.

@github-actions
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📐 language-design Language architecture
Projects
Status: 🤝 Backlog - handoff to owners
Development

No branches or pull requests

5 participants