Add Result.flatten.
Update version to 1.0.0
Fix typo in Result doc forgotten in previous CL.
R=sgjesse@google.com
Review URL: https://codereview.chromium.org//297033002
git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart/pkg/async@36556 260f80e4-7a28-3924-810f-c04153c831b5
diff --git a/lib/result.dart b/lib/result.dart
index f76481c..903c8a5 100644
--- a/lib/result.dart
+++ b/lib/result.dart
@@ -27,7 +27,7 @@
* calling `computation`, or an [ErrorResult] with an error thrown by
* the call.
*/
- Result(T computation()) {
+ factory Result(T computation()) {
try {
return new ValueResult(computation());
} catch (e, s) {
@@ -71,7 +71,7 @@
/**
* Release the result of a captured future.
*
- * Converts the [Result] value of the given [future] to a result or error
+ * Converts the [Result] value of the given [future] to a value or error
* completion of the returned future.
*
* If [future] completes with an error, the returned future completes with
@@ -107,6 +107,19 @@
}
/**
+ * Converts a result of a result to a single result.
+ *
+ * If the result is an error, or it is a `Result` value
+ * which is then an error, then a result with that error is returned.
+ * Otherwise both levels of results are value results, and a single
+ * result with the value is returned.
+ */
+ static Result flatten(Result<Result> result) {
+ if (result.isError) return result;
+ return result.asValue.value;
+ }
+
+ /**
* Whether this result is a value result.
*
* Always the opposite of [isError].
diff --git a/pubspec.yaml b/pubspec.yaml
index 9e5f360..65539f9 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: async
-version: 0.9.2
+version: 1.0.0
author: Dart Team <misc@dartlang.org>
description: Utility functions and classes related to the 'dart:async' library.
homepage: http://www.dartlang.org
diff --git a/test/result_test.dart b/test/result_test.dart
index 53472ec..d101b48 100644
--- a/test/result_test.dart
+++ b/test/result_test.dart
@@ -231,6 +231,26 @@
c.add(new Result.value(37));
c.close();
});
+
+
+ test("flatten error 1", () {
+ Result<int> error = new Result<int>.error("BAD", stack);
+ Result<int> flattened = Result.flatten(error);
+ expectResult(flattened, error);
+ });
+
+ test("flatten error 2", () {
+ Result<int> error = new Result<int>.error("BAD", stack);
+ Result<Result<int>> result = new Result<Result<int>>.value(error);
+ Result<int> flattened = Result.flatten(result);
+ expectResult(flattened, error);
+ });
+
+ test("flatten value", () {
+ Result<Result<int>> result =
+ new Result<Result<int>>.value(new Result<int>.value(42));
+ expectResult(Result.flatten(result), new Result<int>.value(42));
+ });
}
void expectResult(Result actual, Result expected) {