summaryrefslogtreecommitdiff
path: root/lib/std/try.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-08-26 12:20:58 -0700
committerOri Bernstein <ori@eigenstate.org>2015-08-26 12:20:58 -0700
commit2bc852bda98762d3bc01548bf972e3f1b137fbfb (patch)
tree74831deed3c9057c5fe0cbb8790d220e855bc792 /lib/std/try.myr
parent3de952510eb2a23350d24ed926f19c0cf72a12f2 (diff)
downloadmc-2bc852bda98762d3bc01548bf972e3f1b137fbfb.tar.gz
Move Myrddin libs to lib/ subdirectory.
Diffstat (limited to 'lib/std/try.myr')
-rw-r--r--lib/std/try.myr38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/std/try.myr b/lib/std/try.myr
new file mode 100644
index 0000000..da960ac
--- /dev/null
+++ b/lib/std/try.myr
@@ -0,0 +1,38 @@
+use "result.use"
+use "option.use"
+use "fmt.use"
+
+pkg std =
+ generic try : (v : result(@a, @b) -> @a)
+ generic tryv : (v : result(@a, @b), d : @a -> @a)
+ generic get : (v : option(@a) -> @a)
+ generic getv : (v : option(@a), d : @a -> @a)
+;;
+
+generic try = {v
+ match v
+ | `Ok x: -> x
+ | `Fail m: fatal("error: {}\n", m)
+ ;;
+}
+
+generic tryv = {v, d
+ match v
+ | `Ok x: -> x
+ | `Fail m: -> d
+ ;;
+}
+
+generic get = {v
+ match v
+ | `Some x: -> x
+ | `None: fatal("error: option had `None\n")
+ ;;
+}
+
+generic getv = {v, d
+ match v
+ | `Some x: -> x
+ | `None: -> d
+ ;;
+}