Represents a calculation which has succeeded and contains the result. Contains the computation value.
| 409 | |
| 410 | @final |
| 411 | class Success(Result[_ValueType_co, Any]): |
| 412 | """ |
| 413 | Represents a calculation which has succeeded and contains the result. |
| 414 | |
| 415 | Contains the computation value. |
| 416 | """ |
| 417 | |
| 418 | __slots__ = () |
| 419 | |
| 420 | _inner_value: _ValueType_co |
| 421 | |
| 422 | def __init__(self, inner_value: _ValueType_co) -> None: |
| 423 | """Success constructor.""" |
| 424 | super().__init__(inner_value) |
| 425 | |
| 426 | if not TYPE_CHECKING: # noqa: WPS604 # pragma: no branch |
| 427 | |
| 428 | def alt(self, function): |
| 429 | """Does nothing for ``Success``.""" |
| 430 | return self |
| 431 | |
| 432 | def map(self, function): |
| 433 | """Composes current container with a pure function.""" |
| 434 | return Success(function(self._inner_value)) |
| 435 | |
| 436 | def bind(self, function): |
| 437 | """Binds current container to a function that returns container.""" |
| 438 | return function(self._inner_value) |
| 439 | |
| 440 | #: Alias for `bind` method. Part of the `ResultBasedN` interface. |
| 441 | bind_result = bind |
| 442 | |
| 443 | def lash(self, function): |
| 444 | """Does nothing for ``Success``.""" |
| 445 | return self |
| 446 | |
| 447 | def apply(self, container): |
| 448 | """Calls a wrapped function in a container on this container.""" |
| 449 | if isinstance(container, Success): |
| 450 | return self.map(container.unwrap()) |
| 451 | return container |
| 452 | |
| 453 | def value_or(self, default_value): |
| 454 | """Returns the value for successful container.""" |
| 455 | return self._inner_value |
| 456 | |
| 457 | def swap(self): |
| 458 | """Successes swap to :class:`Failure`.""" |
| 459 | return Failure(self._inner_value) |
| 460 | |
| 461 | def unwrap(self) -> _ValueType_co: |
| 462 | """Returns the unwrapped value from successful container.""" |
| 463 | return self._inner_value |
| 464 | |
| 465 | def failure(self) -> Never: |
| 466 | """Raises an exception for successful container.""" |
| 467 | raise UnwrapFailedError(self) |
| 468 |
no outgoing calls