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

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.