OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 /// A type representing values that are either `Future<T>` or `T`. | |
8 /// | |
9 /// This class declaration is a public stand-in for an internal | |
10 /// future-or-value type. References to this class are resolved to the | |
Lasse Reichstein Nielsen
2016/12/08 16:46:52
Technically (ob-xkcd 1475) it's not a *type*, it's
floitsch
2016/12/09 18:19:48
Done.
| |
11 /// internal type. | |
12 /// | |
13 /// It is a compile-time error for any class to extend, mix in or implement | |
14 /// `FutureOr`. | |
15 /// | |
16 /// Note: the `FutureOr<T>` type is interpreted as `dynamic` in non strong-mode. | |
17 /// | |
18 /// # Examples | |
19 /// ``` dart | |
20 /// // The `Future<T>.then` function takes a callback [f] that returns either | |
21 /// // an `S` or a `Future<S>`. | |
22 /// Future<S> then<S>(FutureOr<S> f(T x), ...); | |
23 /// | |
24 /// // `Completer<T>.complete` takes either a `T` or `Future<T>`. | |
25 /// void complete(FutureOr<T> value); | |
26 /// ``` | |
27 /// | |
28 /// # Advanced | |
29 /// The `FutureOr<int>` type is actually the "type union" of the types `int` and | |
30 /// `Future<int>`. This type union is defined in such a way that | |
31 /// `FutureOr<Object>` is both a super- and sub-type of `Object` (sub-type | |
32 /// because `Object` is one of the types of the union, super-type because | |
33 /// `Object` is a super-type of both of the types of the union). Together it | |
34 /// means that `FutureOr<Object>` is equivalent to `Object`. | |
35 /// | |
36 /// As a corollary, `FutureOr<Object>` is equivalent to | |
37 /// `FutureOr<FutureOr<Object>>`, `FutureOr<Future<Object>> is equivalent to | |
38 /// `Future<Object>`. | |
39 abstract class FutureOr<T> { | |
40 // Private constructor, so that it is not subclassable, mixable, or | |
Lasse Reichstein Nielsen
2016/12/08 16:46:52
Silly me, I forgot that a factory constructor does
floitsch
2016/12/09 18:19:48
Done.
| |
41 // instantiable. | |
42 factory FutureOr._() { | |
43 throw new UnsupportedError("FutureOr can't be instantiated"); | |
44 } | |
45 } | |
46 | |
7 /** | 47 /** |
8 * An object representing a delayed computation. | 48 * An object representing a delayed computation. |
9 * | 49 * |
10 * A [Future] is used to represent a potential value, or error, | 50 * A [Future] is used to represent a potential value, or error, |
11 * that will be available at some time in the future. | 51 * that will be available at some time in the future. |
12 * Receivers of a [Future] can register callbacks | 52 * Receivers of a [Future] can register callbacks |
13 * that handle the value or error once it is available. | 53 * that handle the value or error once it is available. |
14 * For example: | 54 * For example: |
15 * | 55 * |
16 * Future<int> future = getFuture(); | 56 * Future<int> future = getFuture(); |
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
780 if (replacement != null) { | 820 if (replacement != null) { |
781 error = _nonNullError(replacement.error); | 821 error = _nonNullError(replacement.error); |
782 stackTrace = replacement.stackTrace; | 822 stackTrace = replacement.stackTrace; |
783 } | 823 } |
784 result._completeError(error, stackTrace); | 824 result._completeError(error, stackTrace); |
785 } | 825 } |
786 | 826 |
787 /** Helper function that converts `null` to a [NullThrownError]. */ | 827 /** Helper function that converts `null` to a [NullThrownError]. */ |
788 Object _nonNullError(Object error) => | 828 Object _nonNullError(Object error) => |
789 (error != null) ? error : new NullThrownError(); | 829 (error != null) ? error : new NullThrownError(); |
OLD | NEW |