Macro for struct generation using symbolic parameters

Hello everyone,

I try to achieve the following, and can’t find a solution. Maybe its not even possible.
I have created a macro, which should help a user to define a certain type of struct. This struct needs a list of parameters, and a function which relies on those parameters.

abstract type MetaObject end
macro createobject(name, parameters, func)
    str = :(

        struct $name <: MetaObject
            para::NamedTuple
            func::Function

            function $name(para)
                para = NamedTuple{$parameters}(para)
                return new(para, $stress)
            end
        end
    )

    return str
end

# Creating a custom object, which could have several instances with different parameters.
@createobject TestObject (:a, :b, :c) function (args)
      #Do stuff with args AND the parameters :a, :b, :c for the concrete instance
end

However, i cant wrap my head around it how i should implement the usage of :a, … in the functioin body. Since it is a macro which is working on an expression, i think of a may to manipulate the expression that it later detects the values, when a concrete object with parameters (1, 2, 3) is created?

Or is it even possible with Julia what i want to achieve/ are they better methods?
Greetings

So, after I couldn’t find a solution the last 2 days, I just figured it out after posting. What seems to work is:

abstract type MetaObject end

macro createobject(name, parameters, func)
    str = :(

        struct $name <: MetaObject
            para::NamedTuple
            func::Function

            function $name(para)
                para = NamedTuple{$parameters}(para)
                return new(para, $stress)
            end
        end
    )

    return str
end

# Creating a custom object, which could have several instances with different parameters.
@createobject TestObject (:a, :b, :c) function (arg)
# just using the parameter field name works fine.
      a = para.a
      b = para.b
      c = para.c

      args + a + b + c
end

However, I don’t really understand why this works at all, maybe there is someone that can explain what is happening?

I cannot run your code because $stress is not defined.

If I am not wrong, the argument to the func parameter is not being called anywhere, so that part of the code is never run (it is taken by the macro and then not used to do anything).

That is correct, indeed. There is a type in my example. This should do it:

abstract type MetaObject end

macro createobject(name, parameters, stress)
    str = :(

        struct $name <: MetaObject
            para::NamedTuple
            func::Function

            function $name(para)
                para = NamedTuple{$parameters}(para)
                return new(para, $stress)
            end
        end
    )

    return str
end

# Creating a custom object, which could have several instances with different parameters.
@createobject TestObject (:a, :b, :c) function (arg)
# just using the parameter field name works fine.
      a = para.a
      b = para.b
      c = para.c

      args + a + b + c
end

The function is meant to run within another function, representing different mechanical material properties. (eg. Linear Elastic, Non-Elastic behavior.)

Well, this codes seems correct if the goal is just to create a new type of object with some name, and with a constructor that always initializes func to the function passed to the macro.