-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadvanced_types.nim
65 lines (57 loc) · 1.74 KB
/
advanced_types.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
#nim compile --run advanced_types.nim
#nim c -r advanced_types.nim
# We can declare new alias types for existing types
type
biggestInt = int64
biggestFloat = float64
var big_number: biggestInt = 123456
echo "big_number = ", big_number
# We can declare enums
type
Direction = enum north, east, south, west
var first_direction = south
echo "first_direction = ", first_direction
var second_direction: Direction = Direction.north
echo "second_direction = ", second_direction
echo "ord(first_direction) = ", ord(first_direction)
# We can create sub-ranges
type
SmallPositiveNumbers = range[1..10]
var pos_number: SmallPositiveNumbers = 1
echo "pos_number = ", pos_number
pos_number = 9
echo "pos_number = ", pos_number
# This will fail because it's not a valid value for 'SmallPositiveNumbers'
#pos_number = 11
# We can create sets
type
CharSet = set[char]
var
c1: CharSet
c2: CharSet
c1 = {'a'..'z', '0'..'9'}
echo "c1 = ", c1
c2 = {'0'..'9'}
echo "c2 = ", c2
# We can do set union
echo "c1 + c2 = ", c1 + c2
# We can do set intersection
echo "c1 * c2 = ", c1 * c2
# We can do set difference
echo "c1 - c2 = ", c1 - c2
# There's a bunch more stuff for sets:
#[
operation meaning
A + B union of two sets
A * B intersection of two sets
A - B difference of two sets (A without B's elements)
A == B set equality
A <= B subset relation (A is subset of B or equal to B)
A < B strict subset relation (A is a proper subset of B)
e in A set membership (A contains element e)
e notin A A does not contain element e
contains(A, e) A contains element e
card(A) the cardinality of A (number of elements in A)
incl(A, elem) same as A = A + {elem}
excl(A, elem) same as A = A - {elem}
]#