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

Unified Diff: tools/gardening_tools/base_lib/lib/src/try.dart

Issue 3005443002: Additional tools for gardening. (Closed)
Patch Set: Removed accidental commit Created 3 years, 4 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: tools/gardening_tools/base_lib/lib/src/try.dart
diff --git a/tools/gardening_tools/base_lib/lib/src/try.dart b/tools/gardening_tools/base_lib/lib/src/try.dart
new file mode 100644
index 0000000000000000000000000000000000000000..bf57c9de1b2aa8b97106716f5d28f3726bba039c
--- /dev/null
+++ b/tools/gardening_tools/base_lib/lib/src/try.dart
@@ -0,0 +1,65 @@
+import 'dart:async';
+
+/// [Try] is similar to Haskell monad, where
+/// a computation may throw an exception.
+/// There is no checking of passing null into
+/// the value, so be mindful.
+class Try<T> {
+ final T _val;
+ final StackTrace _stackTrace;
+ final Exception _err;
+
+ Try.from(this._val)
+ : _err = null,
+ _stackTrace = null;
+
+ Try.error(this._err, this._stackTrace) : _val = null;
+
+ Try<S> bind<S>(S f(T)) {
+ if (_err != null) {
+ return new Try.error(_err, _stackTrace);
+ }
+
+ try {
+ return new Try<S>.from(f(_val));
+ } catch (ex, stackTrace) {
+ return new Try<S>.error(ex, stackTrace);
+ }
+ }
+
+ Future<Try<S>> bindAsync<S>(Future<S> f(T)) async {
Bill Hesse 2017/08/23 15:41:25 Future itself has an error channel and a value cha
mkroghj 2017/08/25 09:28:28 The idea for bindAsync is to change Try<Future<X>>
+ if (_err != null) {
+ return new Try.error(_err, _stackTrace);
+ }
+
+ try {
+ return new Try.from(await f(_val));
+ } catch (ex, stackTrace) {
+ return new Try.error(ex, stackTrace);
+ }
+ }
+
+ void fold(caseErr(Exception), caseVal(T)) {
+ if (_err != null)
Johnni Winther 2017/08/23 12:40:47 Add curly braces (dangling statements are brittle)
+ caseErr(_err);
+ else
+ caseVal(_val);
+ }
+
+ bool isError() => _err != null;
Johnni Winther 2017/08/23 12:40:47 Make this a getter: bool get isError => _error
+
+ Exception getError() => _err;
Johnni Winther 2017/08/23 12:40:47 Ditto.
+ StackTrace getStackTrace() => _stackTrace;
Johnni Winther 2017/08/23 12:40:47 Ditto.
+
+ T get() => _val;
Johnni Winther 2017/08/23 12:40:47 Make this a getter: bool get value => _val;
+
+ T getOrDefault(T defval) => _err != null ? defval : _val;
+}
+
+Try<T> tryStart<T>(T action()) {
+ return new Try<int>.from(0).bind<T>((dummy) => action());
+}
+
+Future<Try<T>> tryStartAsync<T>(Future<T> action()) {
+ return new Try<int>.from(0).bindAsync<T>((dummy) => action());
+}

Powered by Google App Engine
This is Rietveld 408576698