Files
sdk/tests/lib_2/isolate/spawn_generic_test.dart
T
Lasse Reichstein Holst Nielsen 757d806b36 Add type parameter to Isolate.spawn.
This is a strong mode migration that was missed in the earlier rounds.
It allows use of functions that have a more restricted argument type than Object in strong mode
while still ensuring that the argument has a correct type.

Change-Id: Ib00e3f4b4a679c003a992d674c36ef672729b22e
Reviewed-on: https://dart-review.googlesource.com/24540
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Leaf Petersen <leafp@google.com>
2017-12-04 07:11:44 +00:00

61 lines
1.5 KiB
Dart

// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Check that Isolate.spawn is generic.
library spawn_generic;
import "dart:isolate";
import "dart:async";
import "package:async_helper/async_helper.dart";
import "package:expect/expect.dart";
void isomain(num args) {
print(args);
// All is well. No throwing.
}
int _count = 0;
void enter() {
asyncStart();
_count++;
}
bool exit() {
asyncEnd();
return --_count == 0;
}
main() {
var remotePort = new RawReceivePort();
remotePort.handler = (m) {
if (m == null) {
if (exit()) remotePort.close();
} else {
List list = m;
throw new AsyncError(m[0], new StackTrace.fromString(m[1]));
}
};
var port = remotePort.sendPort;
// Explicit type works.
enter();
Isolate.spawn<int>(isomain, 42, onExit: port, onError: port);
enter();
Isolate.spawn<num>(isomain, 42, onExit: port, onError: port);
enter();
Isolate.spawn<num>(isomain, 1.2, onExit: port, onError: port);
enter();
Isolate.spawn<double>(isomain, 1.2, onExit: port, onError: port);
enter();
Isolate.spawn<int>(isomain, null, onExit: port, onError: port);
// Inference gets it right.
enter();
Isolate.spawn(isomain, 42, onExit: port, onError: port);
enter();
Isolate.spawn(isomain, 1.2, onExit: port, onError: port);
enter();
Isolate.spawn(isomain, null, onExit: port, onError: port);
}