Function inside Macro

Hi there,

I am struggling to find a way to combine a macro und a function. Lets start with an MWE of what I want to do:

using QuickTypes
abstract type AbstractModel end
param = (a = 1., b = 2.)

# Create a new Parametric struct that is a subtype of some AbstractModel
macro build(modelname, parameter)
    esc(:( @qmutable_fp $modelname( $parameter::NamedTuple ) <: AbstractModel ) )
#Working fine
@build(MyModel, param)

#1 Now, is there a way to combine @build(MyModel, param) and MyModel(param) such that @build outputs the the actual struct and I don’t need to use both function separately? As soon as I define a new MyModel that is not yet in scope, my solution will not work anymore:

macro build2(modelname, parameter)
    esc( :( @qmutable_fp $modelname( $parameter::NamedTuple) ) )
    #return esc( :( $modelname($param) ) )
    return :( $modelname($param) )
@build2(MyModel, param) #works because already assigned before
@build2(MyModel2, param) #UndefVarError: MyModel2 not defined

@macroexpand( @build2(MyModel2, param) ) # Seems to return exactly what I want, which is a bit puzzling to me

#2 Similarly, lets assume I want to have a specific constructor for modelname(parameter). Unfortunately, I cannot just create a (modelname::AbstractModel)(parameter<:NamedTuple), because the more narrow choice (model::MyModel)(param::NamedTuple) will be chosen and (model::M)(param::NamedTuple) where {M<:AbstractModel} does not seem to work. So I thought I just make a wrapper function:

function init(modelname, parameter)
    #do some stuff
    return modelname(parameter)
init(MyModel, param)

However, just like @build2, the function within the macro does not seem to work here:

macro build3(modelname, parameter)
    esc( :( @qmutable_fp $modelname( $parameter::NamedTuple) ) )
    return :( init($modelname, $param) )
@build3(MyModel3, param) #UndefVarError: MyModel3 not defined

There are quite a few similar questions on discourse, but I did not manage to make my examples work even with the guidance from there unfortunately.