Module rbwr.either

The Either type and its components.

Classes

class Either (adt: tuple[Left, L] | tuple[Right, R])
Expand source code
class Either[L, R]:
    """
    A type that is either a left or a right value, never both.
    """

    def __init__(self, adt: tuple[Left, L] | tuple[Right, R]) -> None:
        self.inner = adt
        """
        The inner value.

        ## Examples

        ```python
        val = Either((Left(), 5))

        match val.inner:
            case (Left(), x):
                assert x == 5
            case (Right(), _):
                raise AssertionError("unreachable")
        ```
        """

    def __repr__(self) -> str:
        match self.inner:
            case (Left(), x):
                return f"Left({repr(x)})"
            case (Right(), x):
                return f"Right({repr(x)})"

    def __str__(self) -> str:
        match self.inner:
            case (Left(), x):
                return str(x)
            case (Right(), x):
                return str(x)

A type that is either a left or a right value, never both.

Ancestors

  • typing.Generic

Instance variables

var inner

The inner value.

Examples

val = Either((Left(), 5))

match val.inner:
    case (Left(), x):
        assert x == 5
    case (Right(), _):
        raise AssertionError("unreachable")
class Left
Expand source code
class Left:
    """
    The "left" variant containing a value.
    """

The "left" variant containing a value.

class Right
Expand source code
class Right:
    """
    The "right" variant containing a value.
    """

The "right" variant containing a value.