Why use metaprogramming?

Hi all

I am planning on doing the following in 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
  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. which seems to use metaprogramming.

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

1 Like
1 Like

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

macro use_socket(addr)
		addr = $(esc(addr))

		msg, set_msg = @PlutoHooks.use_state(nothing)

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

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

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

			() -> close(socket)


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?

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.

See answer here: Why use macros in Julia? - Stack Overflow


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

    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 :slight_smile:

thanks @Jeff_Emanuel

on friday I sat down and watched jfta metaprogramming ( 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 actually works as there are some things that I don’t get. I am making progress thanks to wonderful people like you.


1 Like