Algebraic Data Types in Python
import adt
class MyADT(adt.ADT):
foo: ()
bar: (int,)
baz: (int, bool, str, None)
@adt.fieldmethod
def double_first(field, basecls) -> "MyADT":
if isinstance(field, basecls.foo):
raise TypeError(
f"{basecls.foo.__qualname__} does not support doubling"
)
field_type = type(field)
first, *rem = field
return field_type(first * 2, *rem)
foo = MyADT.foo()
bar = MyADT.bar(4)
baz = MyADT.baz(3, False, "hi", None)
for val in [foo, bar, baz]:
print(val)
try:
doubled = val.double_first()
print("Doubled:", doubled)
except TypeError:
print("Failed to double")
print()
See examples/, adt/example.py and tests/test.py.
Release files for adt 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| adt-0.0.2.tar.gz | 8.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| adt-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.5 kB
Release files / adt-0.0.2.tar.gz
| Download URL | adt-0.0.2.tar.gz |
|---|---|
| Size | 8.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
be7c3fd319affeae73080d6b3b6b09df6e342612ca0a525ed7cf924dd05f7004
|
|
BLAKE2b-256 checksum How to use checksums |
0c9273c650f270f8f2d0f84aa95e35fb0e14a4f25fa987d00b8c1cb6fd856437
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.1
|
Release files / adt-0.0.2-py3-none-any.whl
| Download URL | adt-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9c36d5a1004dec40a2964b944d9cd5d4088742f13a4c0c06075a47c05a46802e
|
|
BLAKE2b-256 checksum How to use checksums |
50e07ea4c68d9379da8d39fb85018eae13795bd74a3fbc0bfb0521e46fab3523
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.1
|