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

Unified Diff: pkg/async/lib/result.dart

Issue 297033002: Add Result.flatten. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Making factory properly factory. Created 6 years, 7 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
« no previous file with comments | « no previous file | pkg/async/pubspec.yaml » ('j') | pkg/async/pubspec.yaml » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/async/lib/result.dart
diff --git a/pkg/async/lib/result.dart b/pkg/async/lib/result.dart
index f76481c2d3a42afc621895442d8b844b2fb678a6..903c8a538aa8417901353ca6d6b1a5cf89f09901 100644
--- a/pkg/async/lib/result.dart
+++ b/pkg/async/lib/result.dart
@@ -27,7 +27,7 @@ abstract class Result<T> {
* 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 @@ abstract class Result<T> {
/**
* 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 @@ abstract class Result<T> {
}
/**
+ * 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].
« no previous file with comments | « no previous file | pkg/async/pubspec.yaml » ('j') | pkg/async/pubspec.yaml » ('J')

Powered by Google App Engine
This is Rietveld 408576698