# Macro for struct generation using symbolic parameters

**URL:** <https://discourse.julialang.org/t/macro-for-struct-generation-using-symbolic-parameters/97693>\
**Category:** General Usage\
**Created:** [April 20, 2023, 10:03am UTC](https://discourse.julialang.org/t/macro-for-struct-generation-using-symbolic-parameters/97693 "2023-04-20T10:03:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bohm/32/48027_2.png) [@bohm](https://discourse.julialang.org/u/bohm)\
**Post date:** [April 20, 2023, 10:03am UTC](https://discourse.julialang.org/t/macro-for-struct-generation-using-symbolic-parameters/97693/1 "2023-04-20T10:03:15Z")

</div>

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.

```julia
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

---

<div class="post-metadata">

**Author:** ![bohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bohm/32/48027_2.png) [@bohm](https://discourse.julialang.org/u/bohm)\
**Post date:** [April 20, 2023, 11:21am UTC](https://discourse.julialang.org/t/macro-for-struct-generation-using-symbolic-parameters/97693/2 "2023-04-20T11:21:20Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 20, 2023, 2:20pm UTC](https://discourse.julialang.org/t/macro-for-struct-generation-using-symbolic-parameters/97693/3 "2023-04-20T14:20:41Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![bohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bohm/32/48027_2.png) [@bohm](https://discourse.julialang.org/u/bohm)\
**Post date:** [April 20, 2023, 4:35pm UTC](https://discourse.julialang.org/t/macro-for-struct-generation-using-symbolic-parameters/97693/4 "2023-04-20T16:35:20Z")

</div>

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

```julia
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.)

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 21, 2023, 12:20am UTC](https://discourse.julialang.org/t/macro-for-struct-generation-using-symbolic-parameters/97693/5 "2023-04-21T00:20:34Z")

</div>

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.
