Replies: 1 comment 1 reply
-
Another way to put this is saying that reassignable variables inflight capturing mechanism creates a non-reassignable version of them. (and then we need to think if it makes sense to the mut/imut primitives as well @Chriscbr, How would you achieve the above result with the current type system? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I've started to wonder whether it could make sense to support a way to reference reassignable variables defined in preflight from inflight scopes. That's a lot of jargon so let's see what that means...
Recall that today, Wing prevents you from using a reassignable variable inflight if it was declared in preflight. The main function of this is to prevent you from writing programs that try to mutate some kind of shared state where no shared state exists:
Wing makes this a compile error because to most readers, it looks like the
count
variable tracks a value, and that functions in the cloud may be able to increment it as they please. But if we were to compile this today (bypassing the error), what we'd find is that the value ofcount
would get inlined into the code offn1
andfn2
, so that when they were invoked, they would get executed withcount
initialized to 5 (and any changes would be forgotten after the functions complete). That's not what most users expect, so the compiler currently raises this as an error. (To solve this in Wing, you could use acloud.Counter
resource instead of a plainnum
instead).But suppose we only access the variable without reassigning to it:
Despite the initial ambiguity of the value of
count
, it's possible to define a simple rule for how this kind of variable access could work across all wing programs -- inside inflight scopes, reassignable variables always take on the last value that they had during preflight. In this case, the last value taken on bycount
during preflight is15
, so15
would be logged by thecloud.Function
.Here's a longer example showing how this might be used in some real-world code:
Limitations
The above example may be reasonable when the reassignable variable is a
num
, but what if it's another kind of capturable type, like a resource? Take the following snippet:Even though the final value of
b
is known to be resolved once all preflight code is executed, the fact thatb
changes means it is unclear what binding information should be added toA
so that whena.handle()
is called, it is granted IAM permissions to access the bucket.I would propose to to restrict this capability initially to simple types. By "simple types", I mean capturable types that do not include resources. For example,
str
,num
,Array<str>
,Set<str>
, enums, simple structs, and so on. (Or alternatively, just any type that can be converted into Json).Beta Was this translation helpful? Give feedback.
All reactions