diff options
author | inoas <mail@inoas.com> | 2023-04-16 18:33:29 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2023-04-19 08:21:15 +0100 |
commit | 9611f00ece4618e1c8d37c510b469b01a2cd6128 (patch) | |
tree | 830e6dc38b9048767f92f1654f9794f49ffc3a8d /src | |
parent | 91b1149cac562a5ef2a01853d3e06b42f3ced0e7 (diff) | |
download | gleam_stdlib-9611f00ece4618e1c8d37c510b469b01a2cd6128.tar.gz gleam_stdlib-9611f00ece4618e1c8d37c510b469b01a2cd6128.zip |
deprecate result.then in favour of result.try
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/result.gleam | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam index e64e96a..35c2d00 100644 --- a/src/gleam/result.gleam +++ b/src/gleam/result.gleam @@ -137,26 +137,36 @@ pub fn flatten(result: Result(Result(a, e), e)) -> Result(a, e) { /// ## Examples /// /// ```gleam -/// > then(Ok(1), fn(x) { Ok(x + 1) }) +/// > try(Ok(1), fn(x) { Ok(x + 1) }) /// Ok(2) /// ``` /// /// ```gleam -/// > then(Ok(1), fn(x) { Ok(#("a", x)) }) +/// > try(Ok(1), fn(x) { Ok(#("a", x)) }) /// Ok(#("a", 1)) /// ``` /// /// ```gleam -/// > then(Ok(1), fn(_) { Error("Oh no") }) +/// > try(Ok(1), fn(_) { Error("Oh no") }) /// Error("Oh no") /// ``` /// /// ```gleam -/// > then(Error(Nil), fn(x) { Ok(x + 1) }) +/// > try(Error(Nil), fn(x) { Ok(x + 1) }) /// Error(Nil) /// ``` /// -pub fn then( +/// `result.try` can be utilized the way +/// ancient Gleam's `try` used to work: +/// +/// ```text +/// // Ancient try +/// try file = open_file() +/// // ... can be mimicked like so: +/// use file <- try(open_file()) +/// ``` +/// +pub fn try( result: Result(a, e), apply fun: fn(a) -> Result(b, e), ) -> Result(b, e) { @@ -166,6 +176,16 @@ pub fn then( } } +/// DEPRECATED: Use `result.try` instead, +/// which has the same signature and does the same thing. +/// +pub fn then( + result: Result(a, e), + apply fun: fn(a) -> Result(b, e), +) -> Result(b, e) { + try(result, fun) +} + /// Extracts the `Ok` value from a result, returning a default value if the result /// is an `Error`. /// |