# Why use metaprogramming?

**URL:** <https://discourse.julialang.org/t/why-use-metaprogramming/89626>\
**Category:** New to Julia\
**Tags:** macros, metaprogramming\
**Created:** [November 1, 2022, 6:25pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626 "2022-11-01T18:25:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![1153dent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1153dent/32/43968_2.png) [@1153dent](https://discourse.julialang.org/u/1153dent)\
**Post date:** [November 1, 2022, 6:25pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/1 "2022-11-01T18:25:10Z")

</div>

Hi all

I am planning on doing the following in [PlutoHooks.jl](https://github.com/JuliaPluto/PlutoHooks.jl)

1. stream structured IPM’s into a ZMQ pipe
2. have the IPM’s processed in a Pluto notebook cell using Plutohooks.jl
3. the IPM data will be used to update ( in real time) a dataframe using [DataFramesMeta.jl](https://juliadata.github.io/DataFramesMeta.jl/stable/)
4. the dataframe will be used ( using Plutohooks) to update cells throughout the notebook
5. the notebook will also trigger events using the web api feature of Pluto.jl

I learn from looking at worked examples and someone pointed me at [this example code.](https://pangoraw.github.io/Notebooks/zmq_socket) which seems to use metaprogramming.

**I don’t really understand what the advantage of using metaprogramming is?**

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 1, 2022, 6:43pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/2 "2022-11-01T18:43:18Z")

</div>

> <https://github.com/JuliaPluto/PlutoHooks.jl/blob/main/src/notebook.jl>

---

<div class="post-metadata">

**Author:** ![1153dent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1153dent/32/43968_2.png) [@1153dent](https://discourse.julialang.org/u/1153dent)\
**Post date:** [November 1, 2022, 6:46pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/3 "2022-11-01T18:46:27Z")

</div>

Hi there @jling  
thank you for the reply I just spotted that the file had been truncated and that there was MUCH more to your code. Sorry about that but I still don’t see what the advantage of using metaprogramming is.

I have an idea as to WHAT [metaprogramming](https://towardsdatascience.com/metaprogramming-in-julia-a-full-overview-2b4e811f1f77) is but I do NOT have a great feel for **why** I should use it. So in my OP I gave my usecase and some sample code that would process a ZMQ datastream using **Quote/end** just can’t see **why?** I want to stress I am NOT criticizing the code I just don’t get it. I can work through the rest of the code but the metaprogramming part is a stumbling block for me and I think I might be missing something VERY important.

```julia
macro use_socket(addr)
	quote
		addr = $(esc(addr))

		msg, set_msg = @PlutoHooks.use_state(nothing)

		@PlutoHooks.use_effect([addr]) do
			socket = Socket(REP)
			bind(socket, addr)

			Task() do
				try
					while isopen(socket)
						data = recv(socket, String)
						set_msg(data)

						send(socket, "ok")
					end
				catch ex
					println(stdout, ex)
					@warn "Error in task" exception=(ex, catch_backtrace())
				end
			end |> schedule

			() -> close(socket)
		end

		msg
	end
end

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [November 1, 2022, 7:16pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/4 "2022-11-01T19:16:13Z")

</div>

Are you asking about this specific example you’ve given or are you actually interested in the much broader question of why one would ever use metaprogramming in Julia?

---

<div class="post-metadata">

**Author:** ![1153dent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1153dent/32/43968_2.png) [@1153dent](https://discourse.julialang.org/u/1153dent)\
**Post date:** [November 1, 2022, 7:34pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/5 "2022-11-01T19:34:07Z")

</div>

hi there @johnmyleswhite  
thank you for the reply. I am interested in the broader question. what advantage does metaprogramming offer and I was giving a code example to illustrate my question. It would seem to me that the code could process the ZMQ message flow WITHOUT the quote/end construct. To me the code is a perfect working example of EXACTLY what I want to achieve and the coder took the time to give an excellent MWE so the metaprogramming is there for a reason. I just can’t figure out why.

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [November 1, 2022, 7:46pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/6 "2022-11-01T19:46:50Z")

</div>

See answer here: [Why use macros in Julia? - Stack Overflow](https://stackoverflow.com/questions/58137512/why-use-macros-in-julia)

---

<div class="post-metadata">

**Author:** ![1153dent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1153dent/32/43968_2.png) [@1153dent](https://discourse.julialang.org/u/1153dent)\
**Post date:** [November 1, 2022, 7:56pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/7 "2022-11-01T19:56:46Z")

</div>

@Jeff_Emanuel first of all EXCELLENT Link thanks.  
secondly I don’t see how any of the points in the link address the code block in my OP.

```julia

    compile time copy-and-paste: You don't have to write the same piece of code multiple times but instead can define a short macro that writes it for you wherever you put it. (example)

    domain specific language (DSL): You can create special syntax that after the macros code -> code transform is replaced by pure Julia constructs. This is used in many packages to define special syntax, for example here and here.

    code generation: Imagine you want to write a really long piece of code which, although being long, is very simple because it has some kind of pattern that repeats itself rather trivially. Writing that code by hand can be a pain (or even practically impossible). 

```

third ( and I haven’t watched it but will) the link ends up by referencing [most of the time don’t use metaprogramming](https://www.youtube.com/watch?v=mSgXWpvQEHE&t=579s) 🙂

---

<div class="post-metadata">

**Author:** ![1153dent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1153dent/32/43968_2.png) [@1153dent](https://discourse.julialang.org/u/1153dent)\
**Post date:** [November 5, 2022, 7:21pm UTC](https://discourse.julialang.org/t/why-use-metaprogramming/89626/8 "2022-11-05T19:21:12Z")

</div>

thanks @Jeff_Emanuel

on friday I sat down and watched [jfta metaprogramming](https://www.youtube.com/watch?v=0oYYWNO3Us8) ( my goto guy for all things julia) and then went through your link. I have an understanding now of the basics of metaprogramming in julia.

thanks @jling

for the link which I went through.

I STILL have a way’s to go understanding how [pluto code segment](https://pangoraw.github.io/Notebooks/zmq_socket) actually works as there are some things that I don’t get. I am making progress thanks to wonderful people like you.

dent
