Skip to content

Lose generic information across isolate #40919

@gaaclarke

Description

@gaaclarke

code:

import 'dart:isolate';

void _process<T, U>(SendPort sendPort) async {
  var receivePort = new ReceivePort();
  sendPort.send(receivePort.sendPort);
  U Function(T input) mapper;
  await for (dynamic input in receivePort) {
    if (mapper == null) {
      mapper = input;
    } else {
      sendPort.send(mapper(input as T));
    }
  }
}

Stream<U> pmap<T, U>(List<T> list, U Function(T input) mapper, {int parallel = 1}) async* {
  ReceivePort receivePort = new ReceivePort();
  Isolate isolate = await Isolate.spawn(_process, receivePort.sendPort);
  SendPort sendPort;
  await for (dynamic result in receivePort) {
    if (sendPort == null) {
      sendPort = result;
      sendPort.send(mapper);
      for (T item in list) {
        sendPort.send(item);
      }
    } else {
      yield result;
    }
  }
  isolate.kill();
}

int mapper(int x) => x * x;

void main() async {
  List<int> foo = [1, 2, 3, 4];
  Stream<int> results = pmap(foo, mapper);
  await for (int value in results) {
    print(value);
  }
}

Runtime exception:
type '(int) => int' is not a subtype of type '(dynamic) => dynamic' is generated when we execute mapper = input;

Expected:
Ideally I could say something like: Isolate.spawn(_process<T, U>, receivePort.sendPort); so I wouldn't lose that type information when the function is executed in the other isolate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-isolatetype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions