Difficulty with converting a macro to use multiple parameters

macros

#1

I have a simple macro like this:

macro logit(signal)
        :(subscribe!(a ->@error(@sprintf("Debug %s: ", $(string(signal))), a), $(esc(signal))))
end
using MicroLogging
using ReactiveBasics
as = Signal(0)
@logit as
push!(as, 1)

This works quite well.

However, I like to debug multiple signals. I thought sth like this should work:

macro logit2(signals...)
    for signal in signals
        :(subscribe!(a ->@error(@sprintf("Debug %s: ", $(string(signal))), a), $(esc(signal))))
    end
end
bs = Signal(1)
@logit2 bs bs
push!(bs, 2)

But this does not print anything. Any suggestions on that?


#2

You are not returning anything from the macro. Remember that a macro is something that takes syntax and returns new syntax. You need to create e.g. an Expr(:block) and push! your expressions that you generate into the loop onto its args field and then return that block expression.


#3

Thanks, that did the trick.
Just for reference, this what I got now:

macro logit2(signals...)
    block_exp = Expr(:block)
    for signal in signals
        push!(block_exp.args, :(subscribe!(a ->@error(@sprintf("Debug %s: ", $(string(signal))), a), $(esc(signal)))))
    end
    block_exp
end