result type
{{Use dmy dates|date=October 2023}}
{{More citations needed|date=January 2021|bot=noref (GreenC bot)}}
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 {{code|2=elm|1=type Result e v = Ok v {{!}} Err e}}.{{cite web |title=Result · An Introduction to Elm |url=https://guide.elm-lang.org/error_handling/result.html |url-status=live |archive-url=https://web.archive.org/web/20231009032650/https://guide.elm-lang.org/error_handling/result.html |archive-date=2023-10-09 |access-date=2023-10-09 |website=guide.elm-lang.org}}
- In Haskell, by convention the {{code|Either|haskell}} type is used for this purpose, which is defined by the standard library as {{code|1=data Either a b = Left a {{!}} Right b|2=haskell}}, where {{code|a|haskell}} is the error type and {{code|b|haskell}} is the return type.{{cite web |date=2023-09-22 |title=Data.Either |url=https://hackage.haskell.org/package/base-4.18.1.0/docs/Data-Either.html |url-status=live |archive-url=https://web.archive.org/web/20231009031549/https://hackage.haskell.org/package/base-4.18.1.0/docs/Data-Either.html |archive-date=2023-10-09 |access-date=2023-10-09 |website=hackage.haskell.org}}
- In Kotlin, it is defined by the standard library as {{code|2=kotlin|1=value class Result
}}.{{cite web |title=Result - Kotlin Programming Language |url=https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/ |url-status=live |archive-url=https://web.archive.org/web/20231009032856/https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/ |archive-date=2023-10-09 |access-date=2023-10-09 |website=kotlinlang.org}} - In OCaml, it is defined by the standard library as {{code|2=ocaml|1=type ('a, 'b) result = Ok of 'a {{!}} Error of 'b type}}.{{cite web |title=Error Handling · OCaml Tutorials |url=https://ocaml.org/docs/error-handling#using-the-result-type-for-errors |url-status=live |archive-url=https://web.archive.org/web/20231009030828/https://ocaml.org/docs/error-handling#using-the-result-type-for-errors |archive-date=2023-10-09 |access-date=2023-10-09 |website=ocaml.org}}
- In Rust, it is defined by the standard library as {{code|2=rust|1=enum Result
{ Ok(T), Err(E) } }}.{{cite web |title=std::result - Rust |url=https://doc.rust-lang.org/std/result/index.html |url-status=live |archive-url=https://web.archive.org/web/20231009032955/https://doc.rust-lang.org/std/result/index.html |archive-date=2023-10-09 |access-date=2023-10-09 |website=doc.rust-lang.org}}{{cite web |date=2011-10-29 |title=stdlib: Add result module · rust-lang/rust@c1092fb |url=https://github.com/rust-lang/rust/commit/c1092fb6d88efe51e42df3aae2a321cc669e12a0 |url-status=live |archive-url=https://web.archive.org/web/20231009033047/https://github.com/rust-lang/rust/commit/c1092fb6d88efe51e42df3aae2a321cc669e12a0 |archive-date=2023-10-09 |access-date=2023-10-09 |website=github.com}} - In Scala, the standard library also defines an {{code|Either|scala}} type,{{cite web |title=Scala Standard Library 2.13.12 - scala.util.Either |url=https://www.scala-lang.org/api/current/scala/util/Either.html |url-status=live |archive-url=https://web.archive.org/web/20231009032237/https://www.scala-lang.org/api/current/scala/util/Either.html |archive-date=2023-10-09 |accessdate=2023-10-09 |website=www.scala-lang.org}} however Scala also has more conventional exception handling.
- In Swift, it is defined by the standard library as {{code|2=swift|1=@frozen enum Result
where Failure : Error}}.{{cite web |title=Result {{!}} Apple Developer Documentation |url=https://developer.apple.com/documentation/swift/result |url-status=live |archive-url=https://web.archive.org/web/20231009032505/https://developer.apple.com/documentation/swift/result |archive-date=2023-10-09 |access-date=2023-10-09 |website=developer.apple.com}} - In C++, it is defined by the standard library as {{code|2=cpp|1=std::expected
}}.{{cite web |date=2023-08-25 |title=std::expected - cppreference.com |url=https://en.cppreference.com/w/cpp/utility/expected |url-status=live |archive-url=https://web.archive.org/web/20231009033232/https://en.cppreference.com/w/cpp/utility/expected |archive-date=2023-10-09 |access-date=2023-10-09 |website=en.cppreference.com}} - In Python, it is available from third party libraries such as [https://github.com/dry-python/returns returns] and [https://github.com/rustedpy/result result].
= Rust =
The result object has the methods is_ok()
and is_err()
.
const CAT_FOUND: bool = true;
fn main() {
let result = pet_cat();
if result.is_ok() {
println!("Great, we could pet the cat!");
} else {
println!("Oh no, we couldn't pet the cat!");
}
}
fn pet_cat() -> Result<(), String> {
if CAT_FOUND {
Ok(())
} else {
Err(String::from("the cat is nowhere to be found"))
}
}
See also
References
{{Reflist}}