ref: f1858c72d837e2985995140fad77925515d6205f
parent: 61766f9822581c7f7ed44c66ff975517b350987a
author: Ori Bernstein <[email protected]>
date: Thu May 8 10:51:33 EDT 2014
Add 'try' to libstd. This was something I had been thinking about doing, but never did until recently because of a compiler bug. Now it works.
--- a/libstd/Makefile
+++ b/libstd/Makefile
@@ -42,6 +42,7 @@
strsplit.myr \
strstrip.myr \
sys.myr \
+ try.myr \
types.myr \
units.myr \
utf.myr \
--- /dev/null
+++ b/libstd/try.myr
@@ -1,0 +1,13 @@
+use "option.use"
+use "fmt.use"
+
+pkg std =
+ generic try : (v : option(@a) -> @a)
+;;
+
+generic try = {v
+ match v
+ | `Some x: -> x
+ | `None: fatal(1, "expected `Some @a, got `None\n")
+ ;;
+}
--- /dev/null
+++ b/test/data/stdtry-expected
@@ -1,0 +1,1 @@
+expected `Some @a, got `None
--- /dev/null
+++ b/test/stdtry.myr
@@ -1,0 +1,8 @@
+use std
+
+const main = {
+ var x = `std.Some 123
+ std.try(x)
+ x = `std.None
+ std.try(x)
+}
--- a/test/tests
+++ b/test/tests
@@ -112,10 +112,11 @@
B import-type E 0
B helloworld P Hello-世界
B encodechar P 1世界äa
+B stdsearch E 0
+B stdtry C
B strtab C
B catfile C
B stdsort C
-B stdsearch E 0
B strstrip C
B strsplit C
B strfind C