-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclassprep.jl
More file actions
65 lines (52 loc) · 1.4 KB
/
classprep.jl
File metadata and controls
65 lines (52 loc) · 1.4 KB
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
# Helpers to aid in constructing class definitions
struct ClassId end
mutable struct ClassConstructor
statements
param_defs
method_defs
new_args
arguments
ClassConstructor() = new([],[],[],[],:undef)
end
mutable struct ClassInfo
constructor::ClassConstructor
parameters
undefined
nonfunctions
ClassInfo() = new(ClassConstructor(), [], [], [])
end
function make_constructor(name, info, constructor_info::ClassConstructor)
args = constructor_info.arguments
stats = constructor_info.statements
statements = args == :undef ? map(make_def, info.undefined) : stats
arguments = args == :undef ? info.undefined : args
param_defs = constructor_info.param_defs
method_defs = constructor_info.method_defs
new_args = constructor_info.new_args
:(function $name($(arguments...))
this = $name(ClassId())
$(param_defs...)
$(method_defs...)
$(statements...)
this
end)
end
# unrelated to the metadispatch part of the definition
abstract type Object end
struct Method{T <: Object}
func :: Function
end
(m::Method)(args...) = m.func(args...)
class_field_values(obj) =
filter(x->!isa(x, Method), map(x->getfield(obj, x), fieldnames(obj)))
function Base.show(io::IO, obj::T) where T<:Object
print("$T(")
fields = class_field_values(obj)
for i in eachindex(fields)
print(fields[i])
if i != length(fields)
print(",")
end
end
print(")")
end