// Errors as values (issue #87, ADR 0024): std/result and std/option, // `use` chains, results in data and across actors. import std/io import std/list import std/option import std/process.{type proc} import std/result import std/string type ConfigError { Missing(String), Invalid(String, String) } fn describe :: e: ConfigError -> String { match e { Missing(key) -> "missing " <> key , Invalid(key, reason) -> key <> ": " <> reason } } fn lookup :: pairs: List<(String, String)> key: String -> Option { match pairs { [] -> None , [(k, v), ..rest] -> if k == key { Some(v) } else { lookup rest key } } } /// A `use` chain: the first error stops it. fn port :: pairs: List<(String, String)> -> Result { use text <- result.try (lookup pairs "port" |> option.to_result Missing("port")) use n <- result.try (string.to_int text |> result.map_error fn e { Invalid("port", e) }) if n > 0 && n < 65536 { Ok(n) } else { Error(Invalid("port", "out of range")) } } fn render :: r: Result -> String { match r { Ok(n) -> "port " <> show n, Error(e) -> "error: " <> describe e } } /// Nested results, matched in one pattern. fn classify :: r: Result, String> -> String { match r { Ok(Ok(n)) if n < 0 -> "negative" , Ok(Ok(_)) -> "number" , Ok(Error(inner)) -> "inner " <> inner , Error(outer) -> "outer " <> outer } } fn halve :: n: Int -> Result { if n % 2 == 0 { Ok(n / 2) } else { Error(show n <> " is odd") } } fn order :: o: Order -> String { match o { Lt -> "Lt", Eq -> "Eq", Gt -> "Gt" } } pub fn main :: -> () / { use_chains () result_module () option_module () prelude_impls () across_actors () } fn use_chains :: -> () / { io.println (render (port [("port", "8080")])) io.println (render (port [])) io.println (render (port [("port", "80x")])) io.println (render (port [("port", "70000")])) io.println ( classify Ok(Ok(-1)) <> ", " <> classify Ok(Ok(1)) <> ", " <> classify Ok(Error("a")) <> ", " <> classify Error("b") ) } fn result_module :: -> () / { let good: Result = Ok(4) let bad: Result = Error("no") io.println ( show (result.is_ok good) <> " " <> show (result.is_error good) <> " " <> show (result.is_ok bad) <> " " <> show (result.is_error bad) ) io.println ( show (result.map good fn n { n + 1 }) <> " " <> show (result.map bad fn n { n + 1 }) ) io.println ( show (result.map_error bad string.length) <> " " <> show (result.replace_error bad 0) <> " " <> show (result.replace_error good 0) ) io.println ( show (result.try good halve) <> " " <> show (good |> result.try halve |> result.try halve |> result.try halve) ) let outer: Result, String> = Error("outer") io.println ( show (result.flatten Ok(good)) <> " " <> show (result.flatten Ok(bad)) <> " " <> show (result.flatten outer) ) io.println ( show (result.unwrap_or bad 0) <> " " <> show (result.unwrap_or_else bad string.length) <> " " <> show (result.unwrap_or_else good string.length) ) io.println (show (result.expect good "a constant")) io.println (show (result.to_option good) <> " " <> show (result.to_option bad)) let all_ok: Result, String> = result.all [Ok(1), Ok(2)] io.println (show all_ok <> " " <> show (result.all [Ok(1), Error("x"), Error("y")])) io.println (show (result.values [Ok(1), Error("x"), Ok(3)])) io.println (show (list.map [1, 2, 3, 4] halve |> result.values)) } fn option_module :: -> () / { io.println ( show (option.is_some Some(1)) <> " " <> show (option.is_none Some(1)) <> " " <> show (option.is_none None) ) 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) }) ) let absent: Option = None io.println ( show (option.to_result Some(1) "none") <> " " <> show (option.to_result absent "none") ) io.println (show (option.values [Some(1), None, Some(3)])) } fn prelude_impls :: -> () / { let good: Result = Ok(4) let bad: Result = Error("no") io.println ( show (good == Ok(4)) <> " " <> show (good == bad) <> " " <> order (compare good bad) <> " " <> order (compare bad good) <> " " <> order (compare Ok(1) good) ) io.println (show (hash good == hash (halve 8)) <> " " <> show (hash good == hash bad)) io.println (debug bad) } /// Results cross actors like any other value. fn across_actors :: -> () / { let replies = process.subject () let _ = process.spawn fn { process.send replies (halve 10) process.send replies (halve 7) } io.println ( show (process.receive_forever replies) <> " " <> show (process.receive_forever replies) ) } //> port 8080 //> error: missing port //> error: port: not an integer: "80x" //> error: port: out of range //> negative, number, inner a, outer b //> True False False True //> Ok(5) Error(no) //> Error(2) Error(0) Ok(4) //> Ok(2) Error(1 is odd) //> Ok(4) Error(no) Error(outer) //> 0 2 4 //> 4 //> Some(4) None //> Ok([1, 2]) Error(x) //> [1, 3] //> [1, 2] //> True False True //> Some(2) None //> Ok(1) Error(none) //> [1, 3] //> True False Lt Gt Lt //> True False //> Error("no") //> Ok(5) Error(7 is odd)