-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdataclass.edh
65 lines (54 loc) · 1.15 KB
/
dataclass.edh
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
# similar to Python's PEP 557 -- Edh has Data Classes too
# https://www.python.org/dev/peps/pep-0557
# %%
# written as a data class
data D ( x, y= nan, ) {
method __init__(*** _ ) {
this.total = this.x + this.y
}
}
# %%
# it satisfies
d = D( 3, 11 )
assert$ d < D( 3, 17 )
assert$ d == D( 3, 11 )
assert$ d is not D( 3, 11 )
d | show
# %%
# equivalent written as a normal class
class D {
method __init__(
x as this.x,
y as this.y= nan,
) {
this.total = this.x + this.y
}
method __repr__() {
return 'D( ' ++ this.x ++ ', ' ++ this.y ++ ", )"
}
__str__ = __repr__
method __eq__( other ) case other of {
{ { D:d' } } -> {
; | this.x != d'.x -> false
; | this.y != d'.y -> false
true
}
_ -> false
}
method __compare__( other ) case other of {
{ { D:d' } } -> {
case compare( this.x, d'.x ) of {
EQ -> pass
{ [ LT, GT ] } -> { conclusion } -> return conclusion
_ -> return NA
}
case compare( this.y, d'.y ) of {
EQ -> pass
{ [ LT, GT ] } -> { conclusion } -> return conclusion
_ -> return NA
}
EQ
}
_ -> NA
}
}