-
Notifications
You must be signed in to change notification settings - Fork 1
/
README_code.py
74 lines (47 loc) · 1.34 KB
/
README_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# A quick demo
from sumtype import sumtype
class Thing(sumtype):
def Foo(x: int, y: int): ...
def Bar(y: str, hmm: str): ...
def Zap(): ...
foo = Thing.Foo(x=3, y=5) # named
foo
foo.x; foo.y;
bar = Thing.Bar('hello', 'world') # positional
bar
bar.y; bar.hmm
zap = Thing.Zap()
zap
[type(foo), type(bar), type(zap)] # they're different variants of the same type!
# Automatic equality and hashing
foo == foo; foo == bar;
{foo, bar, bar, zap}
# Pattern matching
def do_something(val: Thing):
if val.is_Foo():
print(val.x * val.y)
elif val.is_Bar():
print('The result is', val.y, val.hmm)
elif val.is_Zap():
print('Whoosh!')
else: val.impossible() # throws an error - nice if you like having all cases covered
for val in (foo, bar, zap):
do_something(val)
# Pattern matching 2
f = lambda val: val.match(
Foo = lambda x, y: x * y,
Zap = lambda: 999,
_ = lambda: -1 # default case
)
[f(val) for val in (foo, bar, zap)]
# Updating
foo; foo.replace(x=5)
bar; bar.replace(y='abc', hmm='xyz')
# Value access and conversions
foo.values(); foo.as_tuple(); foo.as_dict()
Thing.from_tuple(('Bar', 'one', 'two'))
# Pickle support
import pickle
vals = [Thing.Foo(1, 2), Thing.Bar('one', 'two'), Thing.Zap()]
vals2 = pickle.loads(pickle.dumps(vals))
vals; vals == vals2