Result type explained
In functional programming, a result type is a monadic type holding a returned value or an error code. They provide an elegant way of handling errors, without resorting to exception handling; when a function that may fail returns a result type, the programmer is forced to consider success or failure paths, before getting access to the expected result; this eliminates the possibility of an erroneous programmer assumption.
Examples
- In Elm, it is defined by the standard library as .[1]
- In Haskell, by convention the type is used for this purpose, which is defined by the standard library as, where is the error type and is the return type.[2]
- In Kotlin, it is defined by the standard library as .[3]
- In OCaml, it is defined by the standard library as .[4]
- In Rust, it is defined by the standard library as .[5] [6]
- In Scala, the standard library also defines an type,[7] however Scala also has more conventional exception handling.
- In Swift, it is defined by the standard library as .[8]
- In C++, it is defined by the standard library as .[9]
- In Python, it is available from third party libraries such as returns and result.
Rust
The result object has the methods is_ok
and is_err
.const CAT_FOUND: bool = true;
fn main
fn pet_cat -> Result<, String>
See also
Notes and References
- Web site: Result · An Introduction to Elm . live . https://web.archive.org/web/20231009032650/https://guide.elm-lang.org/error_handling/result.html . 2023-10-09 . 2023-10-09 . guide.elm-lang.org.
- Web site: 2023-09-22 . Data.Either . live . https://web.archive.org/web/20231009031549/https://hackage.haskell.org/package/base-4.18.1.0/docs/Data-Either.html . 2023-10-09 . 2023-10-09 . hackage.haskell.org.
- Web site: Result - Kotlin Programming Language . live . https://web.archive.org/web/20231009032856/https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/ . 2023-10-09 . 2023-10-09 . kotlinlang.org.
- Web site: Error Handling · OCaml Tutorials . live . https://web.archive.org/web/20231009030828/https://ocaml.org/docs/error-handling#using-the-result-type-for-errors . 2023-10-09 . 2023-10-09 . ocaml.org.
- Web site: std::result - Rust . live . https://web.archive.org/web/20231009032955/https://doc.rust-lang.org/std/result/index.html . 2023-10-09 . 2023-10-09 . doc.rust-lang.org.
- Web site: 2011-10-29 . stdlib: Add result module · rust-lang/rust@c1092fb . live . https://web.archive.org/web/20231009033047/https://github.com/rust-lang/rust/commit/c1092fb6d88efe51e42df3aae2a321cc669e12a0 . 2023-10-09 . 2023-10-09 . github.com.
- Web site: Scala Standard Library 2.13.12 - scala.util.Either . live . https://web.archive.org/web/20231009032237/https://www.scala-lang.org/api/current/scala/util/Either.html . 2023-10-09 . 2023-10-09 . www.scala-lang.org.
- Web site: Result Apple Developer Documentation . live . https://web.archive.org/web/20231009032505/https://developer.apple.com/documentation/swift/result . 2023-10-09 . 2023-10-09 . developer.apple.com.
- Web site: 2023-08-25 . std::expected - cppreference.com . live . https://web.archive.org/web/20231009033232/https://en.cppreference.com/w/cpp/utility/expected . 2023-10-09 . 2023-10-09 . en.cppreference.com.