Result type
In functional programming, a result type is a monadic type holding a returned value or an error code.
≈ 6 min read
Result type. encyclopegia.org, snapshot enwiki-001370101608. /a/result-type
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
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
edit edit source- In C++, it is defined by the standard library as
std::expected<T, E>.[1] - In Elm, it is defined by the standard library as
type Result e v = Ok v | Err e.[2] - In Haskell, by convention the
Eithertype is used for this purpose, which is defined by the standard library asdata Either a b = Left a | Right b, whereais the error type andbis the return type.[3] - In Java, it is not natively in the standard library, but is available from third party libraries. For example, result4j which includes an interface
com.github.sviperll.result4j.Result<R, E>similar to RustResult<T, E>, and vavr includes an interfaceio.vavr.control.Either<L, R>similar to HaskellEither a b. Because Java and Kotlin are cross-compatible, Java can use thekotlin.Result<T>type from Kotlin. - In Kotlin, it is defined by the standard library as
kotlin.Result<T>.[4] - In OCaml, it is defined by the standard library as
type ('a, 'b) result = Ok of 'a | Error of 'b type.[5] - In Python, it is not natively in the standard library, but is available from third party libraries such as returns and result.
- In Rust, it is defined by the standard library as
std::result::Result<T, E>, an enum withOkandErrcases.[6][7] Additional aliases inside other namespaces exist, such asstd::io::Result<T>(which is an alias forstd::result::Result<T, std::io::Error>). - In Scala, the standard library also defines an
Eithertype,[8] however Scala also has more conventional exception handling. - In Swift, it is defined by the standard library as
@frozen enum Result<Success, Failure> where Failure : Error.[9] - In V, the result type is implemented natively using
!Tas the return type of a function. For examplefn my_function() !string { ... }. Error Handling in V.
C++
edit edit sourceThe std::expected<T, E> class uses std::unexpected() to return the type E, and can return T directly.
A tag std::unexpect (of type std::unexpect_t) may be used to directly construct an error state inside a std::expected.[10] A specialization for std::expected<void, E> 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.[11]
Rust
edit edit sourceEnums in Rust are tagged unions, which can be unpacked with strong type checking through pattern matching. The Rust expected type is std::result::Result<T, E>, with additional aliases across the standard library.
Vlang
edit edit sourceThe Error type is an interface for iError.
See also
edit edit sourceReferences
edit edit source- ^ "std::expected - cppreference.com". en.cppreference.com. 25 August 2023. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "Result · An Introduction to Elm". guide.elm-lang.org. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "Data.Either". hackage.haskell.org. 22 September 2023. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "Result - Kotlin Programming Language". kotlinlang.org. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "Error Handling · OCaml Tutorials". ocaml.org. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "std::result - Rust". doc.rust-lang.org. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "stdlib: Add result module · rust-lang/rust@c1092fb". github.com. 29 October 2011. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "Scala Standard Library 2.13.12 - scala.util.Either". www.scala-lang.org. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ "Result | Apple Developer Documentation". developer.apple.com. Archived from the original on 9 October 2023. Retrieved 9 October 2023. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ cppreference.com (19 August 2026). "std::unexpect_t, std::unexpect". cppreference.com. cppreference.com. Wikipedia revision 1370101608, retrieved 2026-11-18
- ^ cppreference.com (19 August 2026). "std::bad_expected_access". cppreference.com. cppreference.com. Wikipedia revision 1370101608, retrieved 2026-11-18
Text derived from https://en.wikipedia.org/w/index.php?oldid=1370101608 under CC BY-SA 4.0; modifications have been made.