Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Unified Diff: pkg/barback/lib/src/utils.dart

Issue 119673002: Add a ScheduledStream class and some stream matchers. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/barback/lib/src/utils.dart
diff --git a/pkg/barback/lib/src/utils.dart b/pkg/barback/lib/src/utils.dart
index a4a59507a00b2a5215218b6a661bda9cc5580d80..0669c3c77e91213a9432300fea0ae679366dd12f 100644
--- a/pkg/barback/lib/src/utils.dart
+++ b/pkg/barback/lib/src/utils.dart
@@ -9,67 +9,70 @@ import 'dart:typed_data';
import 'package:stack_trace/stack_trace.dart';
-/// A pair of values.
-class Pair<E, F> {
- E first;
- F last;
+/// A class that represents a value or an error.
+class FallibleValue<E> {
Bob Nystrom 2014/01/08 00:47:40 What do you think of just calling this "Fallible"?
nweiz 2014/01/08 22:15:09 Done.
+ /// Whether [this] has a [value], as opposed to an [error].
+ final bool hasValue;
- Pair(this.first, this.last);
+ /// Whether [this] has an [error], as opposed to an[value].
Bob Nystrom 2014/01/08 00:47:40 "an[" -> "a ["
nweiz 2014/01/08 22:15:09 Done.
+ bool get hasError => !hasValue;
- String toString() => '($first, $last)';
+ /// The value.
+ ///
+ /// This will be `null` if [this] has an [error].
+ final E _value;
- bool operator==(other) {
- if (other is! Pair) return false;
- return other.first == first && other.last == last;
+ /// The value.
+ ///
+ /// This will throw a [StateError] if [this] has an [error].
+ E get value {
+ if (hasValue) return _value;
+ throw new StateError("FallibleValue has no value.\n"
+ "$_error$_stackTraceSuffix");
}
- int get hashCode => first.hashCode ^ last.hashCode;
-}
-
-/// A class that represents one and only one of two types of values.
-class Either<E, F> {
- /// Whether this is a value of type `E`.
- final bool isFirst;
-
- /// Whether this is a value of type `F`.
- bool get isSecond => !isFirst;
-
- /// The value, either of type `E` or `F`.
- final _value;
+ /// The error.
+ ///
+ /// This will be `null` if [this] has a [value].
+ final _error;
- /// The value of type `E`.
+ /// The error.
///
- /// It's an error to access this is this is of type `F`.
- E get first {
- assert(isFirst);
- return _value;
+ /// This will throw a [StateError] if [this] has a [value].
+ get error {
+ if (hasError) return _error;
+ throw new StateError("FallibleValue has no error.");
}
- /// The value of type `F`.
+ /// The stack trace for [_error].
+ ///
+ /// This will be `null` if [this] has a [value], or if no stack trace was
+ /// provided.
+ final StackTrace _stackTrace;
+
+ /// The stack trace for [error].
///
- /// It's an error to access this is this is of type `E`.
- F get second {
- assert(isSecond);
- return _value;
+ /// This will throw a [StateError] if [this] has a [value].
+ StackTrace get stackTrace {
+ if (hasError) return _stackTrace;
+ throw new StateError("FallibleValue has no error.");
}
- /// Creates an [Either] with type `E`.
- Either.withFirst(this._value)
- : isFirst = true;
+ FallibleValue.withValue(this._value)
Bob Nystrom 2014/01/08 00:47:40 Maybe just: new Fallible.value(...) new Fallible.
nweiz 2014/01/08 22:15:09 That's what I had at first, but those apparently c
+ : hasValue = true;
- /// Creates an [Either] with type `F`.
- Either.withSecond(this._value)
- : isFirst = false;
+ FallibleValue.withError(this._error, [this._stackTrace])
+ : hasValue = false;
- /// Runs [whenFirst] or [whenSecond] depending on the type of [this].
- ///
- /// Returns the result of whichvever function was run.
- match(whenFirst(E value), whenSecond(F value)) {
- if (isFirst) return whenFirst(first);
- return whenSecond(second);
+ String toString() {
+ if (hasValue) return "FallibleValue value: $value";
+ return "FallibleValue error: $error$_stackTraceSuffix";
}
- String toString() => "$_value (${isFirst? 'first' : 'second'})";
+ String get _stackTraceSuffix {
+ if (stackTrace == null) return "";
+ return "\nStack trace:\n${new Chain.from(_stackTrace).terse}";
+ }
}
/// Converts a number in the range [0-255] to a two digit hex string.

Powered by Google App Engine
This is Rietveld 408576698