ref: 527bad788adf057d71faa98ed6e09952395abe50
parent: 11dc97526dc9a7dcc30cb087b27f588c45266f03
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Jun 14 14:54:07 EDT 2015
Make try work ok std.result. I kind of want that generically a bit more.
--- a/libstd/test/try.myr
+++ b/libstd/test/try.myr
@@ -2,5 +2,7 @@
const main = {
var x = `std.Some 123
- std.assert(std.try(x) == 123, "expected 123 from try")
+ var r : std.result(int, byte[:]) = `std.Ok 666
+ std.assert(std.val(x) == 123, "expected 123 from try")
+ std.assert(std.try(r) == 666, "expected 123 from try")
}
--- a/libstd/try.myr
+++ b/libstd/try.myr
@@ -1,13 +1,22 @@
+use "result.use"
use "option.use"
use "fmt.use"
pkg std =
- generic try : (v : option(@a) -> @a)
+ generic try : (v : result(@a, @b) -> @a)
+ generic val : (v : option(@a) -> @a)
;;
generic try = {v
match v
+ | `Ok x: -> x
+ | `Fail m: fatal("error: {}\n", m)
+ ;;
+}
+
+generic val = {v
+ match v
| `Some x: -> x
- | `None: fatal("expected `Some @a, got `None\n")
+ | `None: fatal("error: option had `None\n")
;;
}