Created
April 28, 2024 20:33
-
-
Save mpgxc/6a2bc399a979fc5df495481d6dbfb8f6 to your computer and use it in GitHub Desktop.
A super simples result monad approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Generic, TypeVar, Union, Optional | |
T = TypeVar("T") | |
E = TypeVar("E") | |
class _Ok(Generic[T]): | |
def __init__(self, value: Optional[T] = None) -> None: | |
self.value = value | |
self.is_ok = True | |
class _Err(Generic[E]): | |
def __init__(self, value: E) -> None: | |
self.value = value | |
self.is_ok = False | |
Result = Union[_Ok[T], _Err[E]] | |
def Ok(value: Optional[T] = None) -> _Ok[T]: | |
return _Ok(value) | |
def Err(value: E) -> _Err[E]: | |
return _Err(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment