{
  "id": "enwiki-001370101608",
  "slug": "result-type",
  "title": "Result type",
  "qid": "",
  "lead": "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.",
  "body": "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') } }",
  "categories": [
    "22904253",
    "22913334",
    "23118434",
    "23603337",
    "24043274",
    "30181011"
  ],
  "license": "CC BY-SA 4.0",
  "source_uri": "https://en.wikipedia.org/w/index.php?oldid=1370101608",
  "content_hash": "",
  "created_at": null,
  "updated_at": 1787102100.0,
  "claims": [],
  "contradictions": [],
  "confidence": 0.6,
  "csl": {
    "id": "enwiki-001370101608",
    "type": "webpage",
    "title": "Result type",
    "author": [
      {
        "family": "Anticloud FZ LLE",
        "literal": "Anticloud FZ LLE"
      }
    ],
    "container-title": "encyclopegia.org",
    "publisher": "Anticloud FZ LLE",
    "publisher-place": "Dubai, United Arab Emirates",
    "URL": "https://encyclopegia.org/a/result-type",
    "issued": {
      "date-parts": [
        [
          2026,
          11,
          21
        ]
      ]
    },
    "accessed": {
      "date-parts": [
        [
          2026,
          11,
          21
        ]
      ]
    },
    "license": "CC BY-SA 4.0",
    "DOI": "eo1:enwiki-001370101608",
    "isBasedOn": "https://en.wikipedia.org/w/index.php?oldid=1370101608",
    "note": "Immutable snapshot enwiki-001370101608"
  }
}