erg/compiler/erg_compiler/lib/std/_erg_result.py
Shunsuke Shibayama 2051692350 Split std files
2022-11-27 00:26:15 +09:00

18 lines
450 B
Python

from typing import TypeVar, Union, _SpecialForm, _type_check
class Error:
def __init__(self, message):
self.message = message
T = TypeVar("T")
@_SpecialForm
def Result(self, parameters):
"""Result type.
Result[T] is equivalent to Union[T, Error].
"""
arg = _type_check(parameters, f"{self} requires a single type.")
return Union[arg, Error]
def is_ok(obj: Result[T]) -> bool:
return not isinstance(obj, Error)