You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be really useful to be able to use functions that doesn't use anything from enclosing scope as top-level/static functions.
There is many packages that simplify scheduling tasks in other isolates, such as: isolate, worker_manager, computer, etc...
But they are restricted to use top-level functions.
This behavior will make it much easier to offload work to isolates.
There is also a large field of solutions that would become practical to use, for example "smart" Future/Stream that would execute sequences of computations in isolate until we pass a function with non-empty scope.
In this code all 3 computations are effectively the same.
But only first can be passed to another isolate or converted to callback handle to be used outside of the dart code. (I used PluginUtilities.getCallbackHandle, because I couldn't find another way to determine whether it is possible to transfer the function to another isolate)
voidmain() async {
awaitwrapped();
}
Futurewrapped() {
final result1Future =MyFutureWrapper(Future.value('static')).then(staticComputation);
final result2Future = result1Future.then(main$1).then(main$1closure$1);
final result3Future = result2Future.then(main$2);
final main$3ContextFuture = result3Future.then(main$2closure$1);
/// apply async and sync context to main$3main$3withContext(asyncContext) {
final syncContext = [];
returnFunction.apply(main$3, [...syncContext, ...asyncContext]);
}
/// inject async contextmain$3injectAsyncContext(_) {
final asyncContext =Future.wait([
result1Future,
result2Future,
]);
return asyncContext;
}
return main$3ContextFuture
.then(main$3injectAsyncContext)
.then(main$3withContext);
}
main$1(_) {
returnMyFutureWrapper(Future.value('closure'));
}
main$1closure$1(s) {
print(s);
return s;
}
main$2(_) {
returnMyFutureWrapper(Future.value('await'));
}
/// controversial part:/// depends only on returned value from the last closure,/// but this might be too hard to implement or affect performancemain$2closure$1(s) {
print(s);
return s;
}
/// last part of the function with injected contextmain$3(result1, result2) {
print('$result1 $result2');
}
output:
then onValue: Closure: (dynamic) => dynamic from Function 'staticComputation': static. -1113980921106075405; onError: null null
then onValue: Closure: (dynamic) => dynamic from Function 'main$1': static. 5544770122214873047; onError: null null
then onValue: Closure: (dynamic) => dynamic from Function 'main$1closure$1': static. 4854342079658418062; onError: null null
then onValue: Closure: (dynamic) => dynamic from Function 'main$2': static. 5529825966233543903; onError: null null
then onValue: Closure: (dynamic) => dynamic from Function 'main$2closure$1': static. -4387027789645933195; onError: null null
then onValue: Closure: (dynamic) => Future<List<dynamic>> null; onError: null null
then onValue: Closure: (dynamic) => dynamic null; onError: null null
then onValue: Closure: (dynamic) => dynamic null; onError: Closure: (Object, StackTrace) => void null
static
then onValue: Closure: (dynamic) => Null null; onError: Closure: (dynamic, [StackTrace]) => Null null
closure
then onValue: Closure: (dynamic) => Null null; onError: Closure: (dynamic, [StackTrace]) => Null null
await
then onValue: Closure: (dynamic) => Null null; onError: Closure: (Object, StackTrace) => Null null
then onValue: Closure: (dynamic) => Null null; onError: Closure: (Object, StackTrace) => Null null
static closure
The text was updated successfully, but these errors were encountered:
I would have no problem with local functions that do not close over local variables being sendable to other (same-source) isolates.
There is nothing in the language which prohibits this, it's entirely a restriction on the serialization implementation used by isolate communication.
The language based solution would be something like a "static function" or "const function" declaration which is even canonicalized. Say, allowing a local function declaration to be preceded by static and then the function body cannot refer to any non-static name from the context.
Or a static expression which is evaluated only once, then retains its value, and cannot refer to local names (effectively sugar for a static late final variable).
It would be really useful to be able to use functions that doesn't use anything from enclosing scope as top-level/static functions.
There is many packages that simplify scheduling tasks in other isolates, such as: isolate, worker_manager, computer, etc...
But they are restricted to use top-level functions.
This behavior will make it much easier to offload work to isolates.
There is also a large field of solutions that would become practical to use, for example "smart"
Future
/Stream
that would execute sequences of computations in isolate until we pass a function with non-empty scope.In this code all 3 computations are effectively the same.
But only first can be passed to another isolate or converted to callback handle to be used outside of the dart code. (I used
PluginUtilities.getCallbackHandle
, because I couldn't find another way to determine whether it is possible to transfer the function to another isolate)output:
sample code with behavior close to expected
output:
The text was updated successfully, but these errors were encountered: