# Help with defining macros

**URL:** https://discourse.julialang.org/t/help-with-defining-macros/84206
**Category:** General Usage
**Tags:** question, macros
**Created:** [July 14, 2022, 5:54am UTC](https://discourse.julialang.org/t/help-with-defining-macros/84206 "2022-07-14T05:54:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [July 14, 2022, 5:54am UTC](https://discourse.julialang.org/t/help-with-defining-macros/84206/1 "2022-07-14T05:54:11Z")

</div>

I have a `struct` as follows:

```julia
@kwdef struct Node
    index
    value
    optional_attribute = 0.0
end

@kwdef struct Network
    nodes::Vector{node} = Node[]
end

```

Now, I can define a `Network` object and assign `nodes` to it. However, I want to do it a bit differently using macros. Something like:

```julia
#Initialize a network
V = Network()

#Now fill in the nodes using macro
@node(V, 1, 10) #index=1, value=2
@node(V, 2, 20, optional_attribute=5)
#and so on

```

How do I define this macro `@node`. Also, since `node` belongs to `V`, I should be later able to retrieve it using `V.nodes`. This is probably a trivial question but I have never used macros before hence the question. Any help is appreciated. Thank you.

---

<div class="post-metadata">

### Author: ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)
#### Post date: [July 14, 2022, 6:33am UTC](https://discourse.julialang.org/t/help-with-defining-macros/84206/2 "2022-07-14T06:33:51Z")

</div>

Why would you want to use a macro? The job of macros is to transform expressions before run time.

A function will do the trick.

```julia
node!(V::Network, i,v; kwargs...) = push!(V.nodes, Node(;(index=i,value=v,kwargs...)...))

```

---

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [July 14, 2022, 6:50am UTC](https://discourse.julialang.org/t/help-with-defining-macros/84206/3 "2022-07-14T06:50:16Z")

</div>

I guess you’re right. Your function would do the trick and macro is probably overkill here. I am just learning about macros and was trying to give it a shot here. Is it possible at all here? If yes, how would you do that? (I understand this is a bad use case though as you said)

Thanks.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [July 14, 2022, 7:04am UTC](https://discourse.julialang.org/t/help-with-defining-macros/84206/4 "2022-07-14T07:04:42Z")

</div>

Mandatory link:

[![](https://global.discourse-cdn.com/julialang/original/3X/7/5/759062729d830f12cdfa7374e0be51cc40e42a54.jpeg "JuliaCon 2019 | Keynote: Professor Steven G. Johnson") ](https://www.youtube.com/watch?v=mSgXWpvQEHE&t=580)

---

<div class="post-metadata">

### Author: ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)
#### Post date: [July 14, 2022, 7:07am UTC](https://discourse.julialang.org/t/help-with-defining-macros/84206/5 "2022-07-14T07:07:34Z")

</div>

Haha. OK. Thanks for the link 😅
