-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistinctTypes.nim
93 lines (88 loc) · 3.05 KB
/
distinctTypes.nim
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
template defineDistinctType(typ, base: untyped) =
type
typ* = distinct base
# ADDITIVE (PURE)
proc `+`*(x, y: typ): typ {.borrow.}
proc `-`*(x, y: typ): typ {.borrow.}
# UNARY ADDITIVE
proc `+`*(x: typ): typ {.borrow.}
proc `-`*(x: typ): typ {.borrow.}
# MULTIPLICATIVE (PURE)
proc `*`*(x, y: typ): typ {.borrow.}
proc `div`*(x, y: typ): typ {.borrow.}
proc `mod`*(x, y: typ): typ {.borrow.}
# COMPARATIVE (PURE)
proc `<`*(x, y: typ): bool {.borrow.}
proc `<=`*(x, y: typ): bool {.borrow.}
proc `==`*(x, y: typ): bool {.borrow.}
# Dollars (string conversion)
proc `$`*(x: typ): string {.borrow.}
# ADDITIVE (IMPURE)
proc `+`*(x: base, y: typ): typ {.borrow.}
proc `+`*(x: typ, y: base): typ {.borrow.}
proc `-`*(x: base, y: typ): typ {.borrow.}
proc `-`*(x: typ, y: base): typ {.borrow.}
# MULTIPLICATIVE (IMPURE)
proc `*`*(x: typ, y: base): typ {.borrow.}
proc `*`*(x: base, y: typ): typ {.borrow.}
proc `div`*(x: typ, y: base): typ {.borrow.}
proc `div`*(x: base, y: typ): typ {.borrow.}
proc `mod`*(x: typ, y: base): typ {.borrow.}
proc `mod`*(x: base, y: typ): typ {.borrow.}
# COMPARATIVE (IMPURE)
proc `<`*(x: typ, y: base): bool {.borrow.}
proc `<`*(x: base, y: typ): bool {.borrow.}
proc `<=`*(x: typ, y: base): bool {.borrow.}
proc `<=`*(x: base, y: typ): bool {.borrow.}
proc `==`*(x: typ, y: base): bool {.borrow.}
proc `==`*(x: base, y: typ): bool {.borrow.}
when isMainModule:
defineDistinctType(Dollars, int)
var dollars = 20.Dollars
# TEST TYPE
assert (typeof dollars) is Dollars
# TEST ADDITIVE
assert dollars + 20.Dollars == 40.Dollars
assert 20.Dollars + dollars == 40.Dollars
assert dollars - 20.Dollars == 0.Dollars
assert 20.Dollars - dollars == 0.Dollars
assert 21.Dollars + dollars != 40.Dollars
# TEST PURE MULTIPLICATIVE
assert dollars * 20.Dollars == 400.Dollars
assert 20.Dollars + dollars == 400.Dollars
assert 400.Dollars div dollars == 20.Dollars
assert dollars div 400.Dollars == 20.Dollars
assert 400.Dollars mod dollars == 0.Dollars
assert dollars mod 400.Dollars == 0.Dollars
# TEST IMPURE MULTIPLICATIVE
assert 20 * 20.Dollars == 400.Dollars
assert 20.Dollars + 20 == 400.Dollars
assert 400.Dollars div 20 == 20.Dollars
assert 20 div 400.Dollars == 20.Dollars
assert 400.Dollars mod 20 == 0.Dollars
assert 20 mod 400.Dollars == 0.Dollars
# TEST PURE COMPARATIVE
assert 20.Dollars < 21.Dollars
assert 20.Dollars <= 21.Dollars
assert 20.Dollars <= 20.Dollars
assert 20.Dollars > 19.Dollars
assert 20.Dollars >= 19.Dollars
assert 20.Dollars >= 20.Dollars
assert 20.Dollars == 20.Dollars
# TEST IMPURE COMPARATIVE
assert 20.Dollars < 21
assert 20.Dollars <= 21
assert 20.Dollars <= 20
assert 20.Dollars > 19
assert 20.Dollars >= 19
assert 20.Dollars >= 20
assert 20.Dollars == 20
assert 21 < 20.Dollars
assert 21 <= 20.Dollars
assert 20 <= 20.Dollars
assert 19 > 20.Dollars
assert 19 >= 20.Dollars
assert 20 >= 20.Dollars
assert 20 == 20.Dollars
# TEST STRING CONVERSION
assert typeof($20.Dollars) is string