-
Notifications
You must be signed in to change notification settings - Fork 28
/
element_type_A02_t03.dart
49 lines (44 loc) · 1.5 KB
/
element_type_A02_t03.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright (c) 2023, 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.
/// @assertion We define the union-free type derived from a type T as follows:
/// If T is of the form S? or the form FutureOr<S> then the union-free type
/// derived from T is the union-free type derived from S. Otherwise, the
/// union-free type derived from T is T
///
/// We define the element type of a generator function `f` as follows:
/// Let S be the union-free type derived from the declared return type of f.
/// ...
/// If `f` is an asynchronous generator and `S` implements `Stream<U>` for some
/// `U` then the element type of `f` is `U`.
///
/// @description Check a run-time type of a returned value of an asynchronous
/// generator function
/// @author sgrekhov22@gmail.com
/// @issue 54159
import "dart:async";
import "../../Utils/expect.dart";
void isRuntimeTypeImplementsStream<T>(Object? o) async {
if (o is! Stream<T>) {
throw ExpectException("Not a Stream<$T>: ${o.runtimeType}");
}
List<T> list = await o.toList();
try {
list.addAll(<T>[]);
asyncEnd();
} on TypeError catch (_) {
throw ExpectException("Expected Stream<$T> but found $o");
}
}
FutureOr<Stream<int>?> foo() async* {
yield 1;
yield 2;
yield 3;
}
main() async {
asyncStart(2);
dynamic d = await foo();
FutureOr<Stream<int>> o = d;
isRuntimeTypeImplementsStream<int>(d);
asyncEnd();
}