Skip to content

Instantly share code, notes, and snippets.

@mpgxc
Created April 28, 2024 20:33
Show Gist options
  • Save mpgxc/6a2bc399a979fc5df495481d6dbfb8f6 to your computer and use it in GitHub Desktop.
Save mpgxc/6a2bc399a979fc5df495481d6dbfb8f6 to your computer and use it in GitHub Desktop.
A super simples result monad approach
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