Hello,
I admit, having read How to get the type of a symbol in a macro? I realize that this may be harder than I thought, but perhaps if I describe what I want to achieve, a solution can be made for this specific case.
In Agents.jl we have a macro, that comes directly from this answer: Composition and inheritance: the Julian way - #5 by dfdx that allows us to create “agents” (mutable structs) that have some fields coming from some other structs. Like so:
# The following is within the `Agents` module:
macro agent(name, base, fields)
base_type = Core.eval(@__MODULE__, base)
base_fieldnames = fieldnames(base_type)
base_types = [t for t in base_type.types]
base_fields = [:($f::$T) for (f, T) in zip(base_fieldnames, base_types)]
res = :(mutable struct $(esc(name)) <: AbstractAgent end)
push!(res.args[end].args, base_fields...)
push!(res.args[end].args, map(esc, fields.args)...)
return res
end
and having e.g., also defined sometjhing like
# this is also within `Agents` module
mutable struct GraphAgent
id::Int
pos::Int
end
A user can do
using Agents
@agent MyAgent GraphAgent begin
name::String
end
which would generate a mutable struct MyAgent
with the additional name
field that would also have the fields id, pos
.
Now, because of the usage of @__MODULE__
, this macro can only be used to “inherit” types that are defined within Agents
. We want to extend this to allow using types defined outside Agents
. My thinking is that this would be simple to do if the user also provides the module the types were defined at. My problem is, I do not know how to use multiple dispatch in a macro to actually avchieve that. My idea is to allow the second macro version:
@agent MySecondAgent MyAgent MyModule begin
age::Int
end
that now would allow using any type MyAgent
defined in module MyModule
(which will typically be Main
), and “inhereting” the fields of that type.
I’ve tried to achieve this with several different versions, but I always mess up in ways I don’t understand… I can post here my failed code versions if it helps