Writing this inner constructor with a macro

Thanks a lot for your answer. Unfortunately, that doesn’t seem to work when used as I intend to:

struct Example
    stuff::String
    @build(Example, params)
end

julia> Example(Dict(:stuff => "thing"))
# UndefVarError: new not defined

Whether this is a good idea, rather than say making a parametric type, is another question of course.

Well, I am already doing it (with I think good reason), just copypasting the boilerplate constructor over and over is what I want to abstract away. Not sure how a parametric type would help.

EDIT: Simply escaping new works. Again, thanks a lot for your help :slight_smile:
I think it’s because new() can only be called from inside a struct, and therefore needs to be escaped to be in the right context.

macro build(type, params)
    quote
        function $(esc(type))(params::AbstractDict)
            args = build($(esc(type)), params)
            $(Expr(:call, esc(:new), Expr(:(...), :args)))
        end
    end
end