encyclopegia.org

Result type

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.

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. import std; using std::expected; using std::ifstream; using std::string; using std::stringstream; using std::unexpected; using std::filesystem::path; enum class FileError { MISSING_FILE, NO_PERMISSION, // more errors here }; expected loadConfig(const path& p) noexcept { if (!std::filesystem::exists(p)) { return unexpected(FileError::MISSING_FILE); } ifstream config{p}; stringstream buffer; if (!config.is_open()) { return unexpected(FileError::NO_PERMISSION); } buffer s = loadConfig(p); s.has_value()) { std::println("Config contents: {}", s.value()); } else { switch (s.error) { case FileError::MISSING_FILE: std::println("Error: path {} not valid or missing!", p); break; case FileError::NO_PERMISSION: std::println("Error: no permission to read file at path {}!", p); break; // additional cases... default: std::unreachable(); } } } A tag std::unexpect (of type std::unexpect_t ) may be used to directly construct an error state inside a std::expected . A specialization for std::expected exists for when a function does not return any data upon success. If the value() method is called on a std::expected that contains an error, a std::bad_expected_access exception is thrown. const CAT_FOUND: bool = true; fn main() { let result: Result = pet_cat(); match result { Ok(_) => println!("Great, we could pet the cat!"), Err(error) => println!("Oh no, we couldn't pet the cat: {error}") } } fn pet_cat() -> Result { if CAT_FOUND { Ok(()) } else { Err(String::from("The cat is nowhere to be found!")) } } const cat_found = true fn main() { cat_name := get_pet_cat_name() or { println("Oh no, we couldn't pet the cat!") exit(1) } println('Great, we could pet the cat ' + cat_name) } fn get_pet_cat_name() !string { if cat_found { return 'Max' } else { return error('the cat is nowhere to be found') } }

References

  1. std::expected - cppreference.com · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "std::expected - cppreference.com." "std::expected - cppreference. 2026-11-17. https://en.cppreference.com/w/cpp/utility/expected. Accessed 25 November 2026.

    Chicago "std::expected - cppreference.com." "std::expected - cppreference. 2026-11-17. https://en.cppreference.com/w/cpp/utility/expected.

    APA "std::expected - cppreference. (2026). std::expected - cppreference.com. Retrieved November 25, 2026, from https://en.cppreference.com/w/cpp/utility/expected

  2. std::unexpect_t, std::unexpect · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "std::unexpect_t, std::unexpect." cppreference. 2026-11-17. https://en.cppreference.com/cpp/utility/expected/unexpect_t. Accessed 25 November 2026.

    Chicago "std::unexpect_t, std::unexpect." cppreference. 2026-11-17. https://en.cppreference.com/cpp/utility/expected/unexpect_t.

    APA cppreference. (2026). std::unexpect_t, std::unexpect. Retrieved November 25, 2026, from https://en.cppreference.com/cpp/utility/expected/unexpect_t

  3. std::bad_expected_access · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "std::bad_expected_access." cppreference. 2026-11-17. https://en.cppreference.com/cpp/utility/expected/bad_expected_access. Accessed 25 November 2026.

    Chicago "std::bad_expected_access." cppreference. 2026-11-17. https://en.cppreference.com/cpp/utility/expected/bad_expected_access.

    APA cppreference. (2026). std::bad_expected_access. Retrieved November 25, 2026, from https://en.cppreference.com/cpp/utility/expected/bad_expected_access

  4. Result · An Introduction to Elm · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "Result · An Introduction to Elm." 2026-11-17. https://guide.elm-lang.org/error_handling/result.html. Accessed 25 November 2026.

    Chicago "Result · An Introduction to Elm." 2026-11-17. https://guide.elm-lang.org/error_handling/result.html.

    APA encyclopegia.org. (2026). Result · An Introduction to Elm. Retrieved November 25, 2026, from https://guide.elm-lang.org/error_handling/result.html

  5. Data.Either · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "Data.Either." "Data. 2026-11-17. https://hackage.haskell.org/package/base-4.18.1.0/docs/Data-Either.html. Accessed 25 November 2026.

    Chicago "Data.Either." "Data. 2026-11-17. https://hackage.haskell.org/package/base-4.18.1.0/docs/Data-Either.html.

    APA "Data. (2026). Data.Either. Retrieved November 25, 2026, from https://hackage.haskell.org/package/base-4.18.1.0/docs/Data-Either.html

  6. Result - Kotlin Programming Language · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "Result - Kotlin Programming Language." 2026-11-17. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/. Accessed 25 November 2026.

    Chicago "Result - Kotlin Programming Language." 2026-11-17. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/.

    APA encyclopegia.org. (2026). Result - Kotlin Programming Language. Retrieved November 25, 2026, from https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/

  7. Error Handling · OCaml Tutorials · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "Error Handling · OCaml Tutorials." 2026-11-17. https://ocaml.org/docs/error-handling#using-the-result-type-for-errors. Accessed 25 November 2026.

    Chicago "Error Handling · OCaml Tutorials." 2026-11-17. https://ocaml.org/docs/error-handling#using-the-result-type-for-errors.

    APA encyclopegia.org. (2026). Error Handling · OCaml Tutorials. Retrieved November 25, 2026, from https://ocaml.org/docs/error-handling#using-the-result-type-for-errors

  8. std::result - Rust · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "std::result - Rust." 2026-11-17. https://doc.rust-lang.org/std/result/index.html. Accessed 25 November 2026.

    Chicago "std::result - Rust." 2026-11-17. https://doc.rust-lang.org/std/result/index.html.

    APA encyclopegia.org. (2026). std::result - Rust. Retrieved November 25, 2026, from https://doc.rust-lang.org/std/result/index.html

  9. stdlib: Add result module · rust-lang/rust@c1092fb · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "stdlib: Add result module · rust-lang/rust@c1092fb." 2026-11-17. https://github.com/rust-lang/rust/commit/c1092fb6d88efe51e42df3aae2a321cc669e12a0. Accessed 25 November 2026.

    Chicago "stdlib: Add result module · rust-lang/rust@c1092fb." 2026-11-17. https://github.com/rust-lang/rust/commit/c1092fb6d88efe51e42df3aae2a321cc669e12a0.

    APA encyclopegia.org. (2026). stdlib: Add result module · rust-lang/rust@c1092fb. Retrieved November 25, 2026, from https://github.com/rust-lang/rust/commit/c1092fb6d88efe51e42df3aae2a321cc669e12a0

  10. Scala Standard Library 2.13.12 - scala.util.Either · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "Scala Standard Library 2.13.12 - scala.util.Either." "Scala Standard Library 2. 2026-11-17. https://www.scala-lang.org/api/current/scala/util/Either.html. Accessed 25 November 2026.

    Chicago "Scala Standard Library 2.13.12 - scala.util.Either." "Scala Standard Library 2. 2026-11-17. https://www.scala-lang.org/api/current/scala/util/Either.html.

    APA "Scala Standard Library 2. (2026). Scala Standard Library 2.13.12 - scala.util.Either. Retrieved November 25, 2026, from https://www.scala-lang.org/api/current/scala/util/Either.html

  11. Result | Apple Developer Documentation · retrieved 2026-11-17 · rev 1370101608
    Cite this reference

    MLA "Result | Apple Developer Documentation." 2026-11-17. https://developer.apple.com/documentation/swift/result. Accessed 25 November 2026.

    Chicago "Result | Apple Developer Documentation." 2026-11-17. https://developer.apple.com/documentation/swift/result.

    APA encyclopegia.org. (2026). Result | Apple Developer Documentation. Retrieved November 25, 2026, from https://developer.apple.com/documentation/swift/result

Cite this article

MLA

"Result type." encyclopegia.org, 2026, https://encyclopegia.org/wiki/result-type. Accessed 25 November 2026.

Chicago

"Result type." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/result-type.

APA

encyclopegia.org. (2026). Result type. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/result-type

Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

blood type

stub · references available

Type 69 tank

stub · references available

solar-type star

stub · references available

Adobe Type

stub · references available

Audi Type B

stub · references available

Audi Type G

stub · references available

Audi Type M

stub · references available

Audi Type SS

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1370101608 · 0.5 · snapshot enwiki-001370101608

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.