I am trying to write a macro that defines multiple methods for a type hierarchy. What I am trying to achieve is a way to arbitrarely enumerate a type hierarchy, by defining an order()
method for each struct in the type tree.
macro enum_type(type)
let type = eval(type)
next = [type]
methods = []
counter = 0
while(!isempty(next))
let current_type = pop!(next)
children = subtypes(current_type)
map(t -> push!(next, t), children)
push!(methods, :(order(::$current_type) = $(counter += 1)))
end
end
quote
$(methods...)
end
end
end
The returned expressions do not seem to be evaluated in toplevel. Is there a way to return multiple toplevel expressions?
The desired behaviour would be to create a method for each type in the hierarchy, as an example, consider
@macroexpand @enum_type (AbstractFloat)
Should write a method order(...)
associating an arbitrary number to each type in the type tree starting from AbstractFloat. For now, the expansion of the macro with argument AbstractFloat is
quote
#= none:14 =#
var"#57#order"(::AbstractFloat) = begin
#= none:10 =#
1
end
var"#57#order"(::Float64) = begin
#= none:10 =#
2
end
var"#57#order"(::Float32) = begin
#= none:10 =#
3
end
var"#57#order"(::Float16) = begin
#= none:10 =#
4
end
var"#57#order"(::BigFloat) = begin
#= none:10 =#
5
end
end
But none of the method declaration are being evaluated.
@enum
macro would shadow the@enum
macro from Base. – Plum