Package org.daiitech.naftah.builtin.lang
Interface Result<T,E>
- Type Parameters:
T- the type of the success valueE- the type of the error value
- All Known Implementing Classes:
Result.Error,Result.Ok
A sealed interface representing the result of a computation that may either succeed (Ok)
or fail (Error). Inspired by the Result type in functional programming languages.
- Author:
- Chakib Daii
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionApplies a function that returns anotherResult, effectively flattening the structure.booleanisError()Returnstrueif the result is anError, otherwisefalse.booleanisOk()Returnstrueif the result is anOk, otherwisefalse.Maps the success value to another value using the given function, if present.unwrap()Returns the contained success value if present, or throws aNaftahBugErrorif this is anError.Returns the contained error value if present, or throws aNaftahBugErrorif this is anOk.Returns the success value if this is anOk, otherwise returns the given default.
-
Method Details
-
isOk
boolean isOk()Returnstrueif the result is anOk, otherwisefalse. -
isError
boolean isError()Returnstrueif the result is anError, otherwisefalse. -
unwrap
Returns the contained success value if present, or throws aNaftahBugErrorif this is anError.- Returns:
- the success value
- Throws:
NaftahBugError- if called on an Error
-
unwrapError
Returns the contained error value if present, or throws aNaftahBugErrorif this is anOk.- Returns:
- the error value
- Throws:
NaftahBugError- if called on an Ok
-
unwrapOr
Returns the success value if this is anOk, otherwise returns the given default.- Parameters:
defaultValue- the value to return if this is anError- Returns:
- the success value or the default
-
map
Maps the success value to another value using the given function, if present. If this is anError, the error is propagated.- Type Parameters:
U- the type of the result of the mapping function- Parameters:
mapper- a function to apply to the success value- Returns:
- a new
Resultcontaining the mapped value or the original error
-
flatMap
Applies a function that returns anotherResult, effectively flattening the structure. If this is anError, it is propagated unchanged.- Type Parameters:
U- the type of the value in the returned result- Parameters:
mapper- a function returning aResult- Returns:
- the result of applying the function, or the original error
-