by drdrey · 2y ago · 0 comments
@dataclass
class InvalidResultError:
result: str
def do_something() -> int | InvalidResultError:
result = get_result()
return 42 if result == "a result" else InvalidResultError(result)
Using a dataclass like this seems a little overkill, but it does get you something close to `Either`.I also don't understand the argument against nullable types: you could return `int | None` here, which would be the same as treating a `Maybe` (you have to explicitly consider the case where there is no value)