Skip to content

Instantly share code, notes, and snippets.

@Naddiseo
Last active November 22, 2019 00:33
Show Gist options
  • Save Naddiseo/055eb8b386044df1c1d4542824fe3df2 to your computer and use it in GitHub Desktop.
Save Naddiseo/055eb8b386044df1c1d4542824fe3df2 to your computer and use it in GitHub Desktop.
from enum import Enum
class ADTValue:
__slots__=('adt','value')
def __init__(self, adt, value):
self.adt=adt
self.value=value
def __repr__(self):
return f'{self.adt!r} of {self.value!r}'
class ADT(Enum):
def __call__(self, other):
if not isinstance(other, ADTValue):
print(f"fill values {self}, {other}")
return ADTValue(self, other)
if other.adt == self:
return other.value
return False
class Result(ADT):
Ok = ('T',)
Err = ('E',)
Quasi = {'r', 'err'}
r = Result.Quasi([1, "hi"])
if v := Result.Ok(r):
print("Ok.T=",v)
elif e := Result.Err(r):
print("Err.e=",e)
elif q := Result.Quasi(r):
[qr, qerr]=q
print(f"Quasi.r={qr}, Quasi.err={qerr}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment