forked from jspahrsummers/adt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental support for sealed class
Related to jspahrsummers#45
- Loading branch information
Showing
3 changed files
with
62 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from adt import adt as sealed | ||
from dataclasses import dataclass | ||
from typing import Tuple | ||
|
||
|
||
@sealed | ||
class Sealed1: | ||
EMPTY: None | ||
INTEGER: int | ||
STRING_PAIR: Tuple[str, str] | ||
|
||
|
||
x1 = Sealed1.INTEGER(5) | ||
print(x1.integer()) | ||
x2 = Sealed1.EMPTY() | ||
print(x2.empty()) | ||
x3 = Sealed1.STRING_PAIR("foo", "bar") | ||
print(x3.string_pair()) | ||
|
||
|
||
@dataclass | ||
class Leaf: | ||
data: int | ||
|
||
|
||
@dataclass | ||
class Node: | ||
left: "Tree" | ||
right: "Tree" | ||
|
||
|
||
@sealed | ||
class Tree: | ||
EMPTY: None | ||
LEAF: Leaf | ||
NODE: Node | ||
|
||
|
||
t1 = Tree.EMPTY() | ||
t2 = Tree.LEAF(3) | ||
t3 = Tree.NODE((Tree.LEAF(3), Tree.LEAF(4))) | ||
print(t1, t2, t3) |