-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.jl
66 lines (48 loc) · 1.33 KB
/
schema.jl
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
using VarStructs
# use syntax sugar :)
using VarStructs: U, UN # union and union with nothing
# declare
@var struct Address
country::String
city::UN{String} = nothing
street::UN{String} = nothing
unit::UN{ U{String, Int64} } = nothing
zip::UN{ U{String, Int64} } = nothing
end
@var struct Human
name::String
address::UN{Address} = nothing
id::UN{Int64} = nothing
end
@var struct Animal
name::String
end
# get instance
h1 = Human(name = "Amin") # don't define all the things
h1.address = Address(country = "Canada") # add things later
h2 = Human(name = "Someone", address = Address(country = "RandomCountry", street = "Street 1", zip = 1) )
h2.id = 2
a1 = Animal(name = "lion")
# redeclare!
@var struct Animal
name::String
id::UN{Int64} = nothing
end
a2 = Animal(name = "lion", id = 1)
# Dispatch!
function introduce_yourself(x::Human)
println("My name is $(x.name), and I am from $(x.address.country)")
end
function introduce_yourself(x::Animal)
println("My name is $(x.name), and I live in jungle")
end
introduce_yourself(h1)
introduce_yourself(h2)
introduce_yourself(a1)
introduce_yourself(a2)
# Custom constructor
function Human(n::String)
@info "Address of human with name $n not found"
return Human(name = n, address = Address(country = "Earth"))
end
h3 = Human("UnknownPerson")