I have this macro definition:
julia> macro foreach(f, arr)
quote
isempty($arr) && return ""
mapreduce(*, $arr) do _s
$f(_s)
end
end
end
@foreach (macro with 1 method)
In Julia v0.6 this works:
julia> @foreach(["a", "b", "c"]) do (x)
x
end
"abc"
In Julia v0.7 it doesn’t:
julia> @foreach(["a", "b", "c"]) do (x)
x
end
ERROR: syntax: invalid macro usage "@(foreach(["a", "b", "c"]) do x
# REPL[11], line 2
x
end)"
Why not and how to get it back?
It works for me in 0.7.0-alpha.197
(Why are you writing a macro and not a function here, or for that matter using the built-in foreach
function?)
Related: https://github.com/JuliaLang/julia/issues/15730
@kristoffer.carlsson Awesome, thank you very much. I’m on 0.7.0-alpha.0 – I’ll update my Julia.
@stevengj it’s a helper which needs to do exactly that, a mapreduce
over *
. A real-life example is
@foreach(tweets) do tweet
include_template("app/resources/tweets/views/partials/tweet.flax.html")
end
I think I ended up with the macro a while ago, most likely because Base.foreach
already existed and the functions would have had the same signature. I’ve added a foreachstr
function now but it’s an uglier name.