Type parametrisation and the @agent macro

Is it possible to use type parametrisation with the @agent macro?

This is what I’d like to do:

@agent struct AnAgent(NoSpaceAgent){C} <: AbstractAgent{C <: FixedDecimal}
  balance::C
end

But this syntax clearly does not work since I get the following error:

ERROR: LoadError: MethodError: no method matching namify(::Nothing)

Closest candidates are:
  namify(::Expr)
   @ MacroTools ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:132
  namify(::Symbol)
   @ MacroTools ~/.julia/packages/MacroTools/Cf2ok/src/utils.jl:131

Stacktrace:
 [1] compute_base_fields(base_type_spec::Nothing)
   @ Agents ~/.julia/packages/Agents/8JW8b/src/core/agents.jl:405
 [2] _agent(struct_repr::Expr)
   @ Agents ~/.julia/packages/Agents/8JW8b/src/core/agents.jl:206
 [3] var"@agent"(__source__::LineNumberNode, __module__::Module, struct_repr::Any)
   @ Agents ~/.julia/packages/Agents/8JW8b/src/core/agents.jl:200
in expression starting at REPL[2]:1

I admit I have not really looked at how macros work exactly. I do know it has to do with how the macro functions though but is there a way to make type parametrisation work here?

Yes, put the C before the parenthesis, see here for the full syntax:

Notice that you should not put the C into the AbstractAgent supertype, as it doesn’t have parametric types itself. You can define your own supertype abstract type MySuperAgent{C} <: AbstractAgent instead.

1 Like