| @@ -0,0 +1,143 @@ |
| 1 | +// Errors as values (issue #87, ADR 0024): std/result and std/option, |
| 2 | +// `use` chains, results in data and across actors. |
| 3 | +import std/io |
| 4 | +import std/list |
| 5 | +import std/option |
| 6 | +import std/result |
| 7 | +import std/string |
| 8 | +import std/process.{type proc} |
| 9 | + |
| 10 | +type ConfigError { |
| 11 | + Missing(String) |
| 12 | + Invalid(String, String) |
| 13 | +} |
| 14 | + |
| 15 | +fn describe(e: ConfigError) -> String { |
| 16 | + match e { |
| 17 | + Missing(key) -> "missing " <> key, |
| 18 | + Invalid(key, reason) -> key <> ": " <> reason, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +fn lookup(pairs: List<(String, String)>, key: String) -> Option<String> { |
| 23 | + match pairs { |
| 24 | + [] -> None, |
| 25 | + [(k, v), ..rest] -> if k == key { Some(v) } else { lookup(rest, key) }, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// A `use` chain: the first error stops it. |
| 30 | +fn port(pairs: List<(String, String)>) -> Result<Int, ConfigError> { |
| 31 | + use text <- result.try(lookup(pairs, "port") |> option.to_result(Missing("port"))) |
| 32 | + use n <- result.try(string.to_int(text) |> result.map_error(fn(e) { Invalid("port", e) })) |
| 33 | + if n > 0 && n < 65536 { Ok(n) } else { Error(Invalid("port", "out of range")) } |
| 34 | +} |
| 35 | + |
| 36 | +fn render(r: Result<Int, ConfigError>) -> String { |
| 37 | + match r { |
| 38 | + Ok(n) -> "port " <> show(n), |
| 39 | + Error(e) -> "error: " <> describe(e), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// Nested results, matched in one pattern. |
| 44 | +fn classify(r: Result<Result<Int, String>, String>) -> String { |
| 45 | + match r { |
| 46 | + Ok(Ok(n)) if n < 0 -> "negative", |
| 47 | + Ok(Ok(_)) -> "number", |
| 48 | + Ok(Error(inner)) -> "inner " <> inner, |
| 49 | + Error(outer) -> "outer " <> outer, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +fn halve(n: Int) -> Result<Int, String> { |
| 54 | + if n % 2 == 0 { Ok(n / 2) } else { Error(show(n) <> " is odd") } |
| 55 | +} |
| 56 | + |
| 57 | +fn order(o: Order) -> String { |
| 58 | + match o { Lt -> "Lt", Eq -> "Eq", Gt -> "Gt" } |
| 59 | +} |
| 60 | + |
| 61 | +pub fn main() -> () / <io, proc> { |
| 62 | + use_chains() |
| 63 | + result_module() |
| 64 | + option_module() |
| 65 | + prelude_impls() |
| 66 | + across_actors() |
| 67 | +} |
| 68 | + |
| 69 | +fn use_chains() -> () / <io> { |
| 70 | + io.println(render(port([("port", "8080")]))) |
| 71 | + io.println(render(port([]))) |
| 72 | + io.println(render(port([("port", "80x")]))) |
| 73 | + io.println(render(port([("port", "70000")]))) |
| 74 | + io.println(classify(Ok(Ok(-1))) <> ", " <> classify(Ok(Ok(1))) <> ", " <> classify(Ok(Error("a"))) <> ", " <> classify(Error("b"))) |
| 75 | +} |
| 76 | + |
| 77 | +fn result_module() -> () / <io> { |
| 78 | + let good: Result<Int, String> = Ok(4) |
| 79 | + let bad: Result<Int, String> = Error("no") |
| 80 | + io.println(show(result.is_ok(good)) <> " " <> show(result.is_error(good)) <> " " <> show(result.is_ok(bad)) <> " " <> show(result.is_error(bad))) |
| 81 | + io.println(show(result.map(good, fn(n) { n + 1 })) <> " " <> show(result.map(bad, fn(n) { n + 1 }))) |
| 82 | + io.println(show(result.map_error(bad, string.length)) <> " " <> show(result.replace_error(bad, 0)) <> " " <> show(result.replace_error(good, 0))) |
| 83 | + io.println(show(result.try(good, halve)) <> " " <> show(good |> result.try(halve) |> result.try(halve) |> result.try(halve))) |
| 84 | + let outer: Result<Result<Int, String>, String> = Error("outer") |
| 85 | + io.println(show(result.flatten(Ok(good))) <> " " <> show(result.flatten(Ok(bad))) <> " " <> show(result.flatten(outer))) |
| 86 | + io.println(show(result.unwrap_or(bad, 0)) <> " " <> show(result.unwrap_or_else(bad, string.length)) <> " " <> show(result.unwrap_or_else(good, string.length))) |
| 87 | + io.println(show(result.expect(good, "a constant"))) |
| 88 | + io.println(show(result.to_option(good)) <> " " <> show(result.to_option(bad))) |
| 89 | + let all_ok: Result<List<Int>, String> = result.all([Ok(1), Ok(2)]) |
| 90 | + io.println(show(all_ok) <> " " <> show(result.all([Ok(1), Error("x"), Error("y")]))) |
| 91 | + io.println(show(result.values([Ok(1), Error("x"), Ok(3)]))) |
| 92 | + io.println(show(list.map([1, 2, 3, 4], halve) |> result.values)) |
| 93 | +} |
| 94 | + |
| 95 | +fn option_module() -> () / <io> { |
| 96 | + io.println(show(option.is_some(Some(1))) <> " " <> show(option.is_none(Some(1))) <> " " <> show(option.is_none(None))) |
| 97 | + io.println(show(option.then(Some(4), fn(n) { result.to_option(halve(n)) })) <> " " <> show(option.then(Some(3), fn(n) { result.to_option(halve(n)) }))) |
| 98 | + let absent: Option<Int> = None |
| 99 | + io.println(show(option.to_result(Some(1), "none")) <> " " <> show(option.to_result(absent, "none"))) |
| 100 | + io.println(show(option.values([Some(1), None, Some(3)]))) |
| 101 | +} |
| 102 | + |
| 103 | +fn prelude_impls() -> () / <io> { |
| 104 | + let good: Result<Int, String> = Ok(4) |
| 105 | + let bad: Result<Int, String> = Error("no") |
| 106 | + io.println(show(good == Ok(4)) <> " " <> show(good == bad) <> " " <> order(compare(good, bad)) <> " " <> order(compare(bad, good)) <> " " <> order(compare(Ok(1), good))) |
| 107 | + io.println(show(hash(good) == hash(halve(8))) <> " " <> show(hash(good) == hash(bad))) |
| 108 | + io.println(debug(bad)) |
| 109 | +} |
| 110 | + |
| 111 | +/// Results cross actors like any other value. |
| 112 | +fn across_actors() -> () / <io, proc> { |
| 113 | + let replies = process.subject() |
| 114 | + let _ = process.spawn(fn() { |
| 115 | + process.send(replies, halve(10)) |
| 116 | + process.send(replies, halve(7)) |
| 117 | + }) |
| 118 | + io.println(show(process.receive_forever(replies)) <> " " <> show(process.receive_forever(replies))) |
| 119 | +} |
| 120 | +//> port 8080 |
| 121 | +//> error: missing port |
| 122 | +//> error: port: not an integer: "80x" |
| 123 | +//> error: port: out of range |
| 124 | +//> negative, number, inner a, outer b |
| 125 | +//> True False False True |
| 126 | +//> Ok(5) Error(no) |
| 127 | +//> Error(2) Error(0) Ok(4) |
| 128 | +//> Ok(2) Error(1 is odd) |
| 129 | +//> Ok(4) Error(no) Error(outer) |
| 130 | +//> 0 2 4 |
| 131 | +//> 4 |
| 132 | +//> Some(4) None |
| 133 | +//> Ok([1, 2]) Error(x) |
| 134 | +//> [1, 3] |
| 135 | +//> [1, 2] |
| 136 | +//> True False True |
| 137 | +//> Some(2) None |
| 138 | +//> Ok(1) Error(none) |
| 139 | +//> [1, 3] |
| 140 | +//> True False Lt Gt Lt |
| 141 | +//> True False |
| 142 | +//> Error("no") |
| 143 | +//> Ok(5) Error(7 is odd) |