louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Errors as values (issue #87, ADR 0024): std/result and std/option, |
| 2 | // `use` chains, results in data and across actors. |
| 3 | |
| 4 | import std/io |
| 5 | import std/list |
| 6 | import std/option |
| 7 | import std/process.{type proc} |
| 8 | import std/result |
| 9 | import std/string |
| 10 | |
| 11 | type ConfigError { Missing(String), Invalid(String, String) } |
| 12 | |
| 13 | fn describe :: e: ConfigError -> String |
| 14 | { |
| 15 | match e |
| 16 | { Missing(key) -> "missing " <> key |
| 17 | , Invalid(key, reason) -> key <> ": " <> reason |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | fn lookup :: pairs: List<(String, String)> key: String -> Option<String> |
| 22 | { |
| 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 | { |
| 32 | use text <- |
| 33 | result.try (lookup pairs "port" |> option.to_result Missing("port")) |
| 34 | use n <- |
| 35 | result.try |
| 36 | (string.to_int text |> result.map_error fn e { Invalid("port", e) }) |
| 37 | if n > 0 && n < 65536 |
| 38 | { |
| 39 | Ok(n) |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | Error(Invalid("port", "out of range")) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | fn render :: r: Result<Int, ConfigError> -> String |
| 48 | { |
| 49 | match r { Ok(n) -> "port " <> show n, Error(e) -> "error: " <> describe e } |
| 50 | } |
| 51 | |
| 52 | /// Nested results, matched in one pattern. |
| 53 | fn classify :: r: Result<Result<Int, String>, String> -> String |
| 54 | { |
| 55 | match r |
| 56 | { Ok(Ok(n)) if n < 0 -> "negative" |
| 57 | , Ok(Ok(_)) -> "number" |
| 58 | , Ok(Error(inner)) -> "inner " <> inner |
| 59 | , Error(outer) -> "outer " <> outer |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn halve :: n: Int -> Result<Int, String> |
| 64 | { |
| 65 | if n % 2 == 0 |
| 66 | { |
| 67 | Ok(n / 2) |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | Error(show n <> " is odd") |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn order :: o: Order -> String |
| 76 | { |
| 77 | match o { Lt -> "Lt", Eq -> "Eq", Gt -> "Gt" } |
| 78 | } |
| 79 | |
| 80 | pub fn main :: -> () / <io, proc> |
| 81 | { |
| 82 | use_chains () |
| 83 | result_module () |
| 84 | option_module () |
| 85 | prelude_impls () |
| 86 | across_actors () |
| 87 | } |
| 88 | |
| 89 | fn use_chains :: -> () / <io> |
| 90 | { |
| 91 | io.println (render (port [("port", "8080")])) |
| 92 | io.println (render (port [])) |
| 93 | io.println (render (port [("port", "80x")])) |
| 94 | io.println (render (port [("port", "70000")])) |
| 95 | io.println |
| 96 | ( classify Ok(Ok(-1)) <> ", " <> classify Ok(Ok(1)) <> ", " |
| 97 | <> classify Ok(Error("a")) |
| 98 | <> ", " |
| 99 | <> classify Error("b") |
| 100 | ) |
| 101 | } |
| 102 | |
| 103 | fn result_module :: -> () / <io> |
| 104 | { |
| 105 | let good: Result<Int, String> = Ok(4) |
| 106 | let bad: Result<Int, String> = Error("no") |
| 107 | io.println |
| 108 | ( show (result.is_ok good) <> " " <> show (result.is_error good) <> " " |
| 109 | <> show (result.is_ok bad) |
| 110 | <> " " |
| 111 | <> show (result.is_error bad) |
| 112 | ) |
| 113 | io.println |
| 114 | ( show (result.map good fn n { n + 1 }) <> " " |
| 115 | <> show (result.map bad fn n { n + 1 }) |
| 116 | ) |
| 117 | io.println |
| 118 | ( show (result.map_error bad string.length) <> " " |
| 119 | <> show (result.replace_error bad 0) |
| 120 | <> " " |
| 121 | <> show (result.replace_error good 0) |
| 122 | ) |
| 123 | io.println |
| 124 | ( show (result.try good halve) <> " " |
| 125 | <> show |
| 126 | (good |> result.try halve |> result.try halve |> result.try halve) |
| 127 | ) |
| 128 | let outer: Result<Result<Int, String>, String> = Error("outer") |
| 129 | io.println |
| 130 | ( show (result.flatten Ok(good)) <> " " <> show (result.flatten Ok(bad)) |
| 131 | <> " " |
| 132 | <> show (result.flatten outer) |
| 133 | ) |
| 134 | io.println |
| 135 | ( show (result.unwrap_or bad 0) <> " " |
| 136 | <> show (result.unwrap_or_else bad string.length) |
| 137 | <> " " |
| 138 | <> show (result.unwrap_or_else good string.length) |
| 139 | ) |
| 140 | io.println (show (result.expect good "a constant")) |
| 141 | io.println |
| 142 | (show (result.to_option good) <> " " <> show (result.to_option bad)) |
| 143 | let all_ok: Result<List<Int>, String> = result.all [Ok(1), Ok(2)] |
| 144 | io.println |
| 145 | (show all_ok <> " " <> show (result.all [Ok(1), Error("x"), Error("y")])) |
| 146 | io.println (show (result.values [Ok(1), Error("x"), Ok(3)])) |
| 147 | io.println (show (list.map [1, 2, 3, 4] halve |> result.values)) |
| 148 | } |
| 149 | |
| 150 | fn option_module :: -> () / <io> |
| 151 | { |
| 152 | io.println |
| 153 | ( show (option.is_some Some(1)) <> " " <> show (option.is_none Some(1)) |
| 154 | <> " " |
| 155 | <> show (option.is_none None) |
| 156 | ) |
| 157 | io.println |
| 158 | ( show (option.then Some(4) fn n { result.to_option (halve n) }) <> " " |
| 159 | <> show (option.then Some(3) fn n { result.to_option (halve n) }) |
| 160 | ) |
| 161 | let absent: Option<Int> = None |
| 162 | io.println |
| 163 | ( show (option.to_result Some(1) "none") <> " " |
| 164 | <> show (option.to_result absent "none") |
| 165 | ) |
| 166 | io.println (show (option.values [Some(1), None, Some(3)])) |
| 167 | } |
| 168 | |
| 169 | fn prelude_impls :: -> () / <io> |
| 170 | { |
| 171 | let good: Result<Int, String> = Ok(4) |
| 172 | let bad: Result<Int, String> = Error("no") |
| 173 | io.println |
| 174 | ( show (good == Ok(4)) <> " " <> show (good == bad) <> " " |
| 175 | <> order (compare good bad) |
| 176 | <> " " |
| 177 | <> order (compare bad good) |
| 178 | <> " " |
| 179 | <> order (compare Ok(1) good) |
| 180 | ) |
| 181 | io.println |
| 182 | (show (hash good == hash (halve 8)) <> " " <> show (hash good == hash bad)) |
| 183 | io.println (debug bad) |
| 184 | } |
| 185 | |
| 186 | /// Results cross actors like any other value. |
| 187 | fn across_actors :: -> () / <io, proc> |
| 188 | { |
| 189 | let replies = process.subject () |
| 190 | let _ = |
| 191 | process.spawn |
| 192 | fn |
| 193 | { |
| 194 | process.send replies (halve 10) |
| 195 | process.send replies (halve 7) |
| 196 | } |
| 197 | io.println |
| 198 | ( show (process.receive_forever replies) <> " " |
| 199 | <> show (process.receive_forever replies) |
| 200 | ) |
| 201 | } |
| 202 | //> port 8080 |
| 203 | //> error: missing port |
| 204 | //> error: port: not an integer: "80x" |
| 205 | //> error: port: out of range |
| 206 | //> negative, number, inner a, outer b |
| 207 | //> True False False True |
| 208 | //> Ok(5) Error(no) |
| 209 | //> Error(2) Error(0) Ok(4) |
| 210 | //> Ok(2) Error(1 is odd) |
| 211 | //> Ok(4) Error(no) Error(outer) |
| 212 | //> 0 2 4 |
| 213 | //> 4 |
| 214 | //> Some(4) None |
| 215 | //> Ok([1, 2]) Error(x) |
| 216 | //> [1, 3] |
| 217 | //> [1, 2] |
| 218 | //> True False True |
| 219 | //> Some(2) None |
| 220 | //> Ok(1) Error(none) |
| 221 | //> [1, 3] |
| 222 | //> True False Lt Gt Lt |
| 223 | //> True False |
| 224 | //> Error("no") |
| 225 | //> Ok(5) Error(7 is odd) |