ballerina-platform/ballerina-spec

Closures need to be isolated wrt a stack frame

Opened this issue · 1 comments

There is a complication with closures and isolated. Consider this:

isolated function arrayInc(int[] v, int k) returns int[] {
  return v.map(n => n + k);
}

This ought to work, but the closure passed to map is not isolated, because it accesses state outside its (the closure’s) parameters, namely k. From a type-checking point of the concept needs to be isolated wrt a specific stack frame. Declaring a function isolated means that is isolated wrt its own stack frame. The closure passed to map is isolated with respect to arrayInc’s stack frame. So the call to map is isolated with respect to arrayInc’s stack frame, so it’s OK for arrayInc to call it.

This also means we ought to infer the isolatedness of anoymous functions values within a function.

Originally posted by @jclark in #145 (comment)

How does this interact with the following from the spec?

Captured variables are restricted as follows: if the body is part of an anonymous-function-expr, then any captured variable references must refer to variables that are both final (either explicitly or implicitly) and have a static type that is a subtype of readonly|isolated object {}.