macro collect(T,expr::Expr)
quote
let v = Vector{$T}()
function $(esc(:remember))(x)
push!(v, x)
end
$(esc(expr))
v
end
end
end
julia> @macroexpand @collect(Float64,remember(42))
quote
#= REPL[4]:3 =#
let var"#5#v" = Main.Vector{Main.Float64}()
#= REPL[4]:4 =#
function remember(var"#7#x")
#= REPL[4]:5 =#
Main.push!(var"#5#v", var"#7#x")
end
#= REPL[4]:7 =#
remember(42)
#= REPL[4]:8 =#
var"#5#v"
end
end
Maybe it would help if you gave a little more detail of what you are trying to achieve. I have a feeling, there might be easier ways to achieve what you are aiming for.
Why not do [x for x in v if x < 7]? And if you want a specific type, do e.g. Int[x for x in v if x < 7 && isinteger(x)]?
(90% of the time, if you are writing a macro or doing some other form of metaprogramming, you are making a mistake. In the remaining 10% of cases it is an extremely useful technique.)
yes but i want to do this using macro … the Int64 is an example so i want to make the user able to specify the element type of the vector that is returned.
If you want to pick out the integers, you should use isinteger which finds numbers that are “numerically equal” to some integer, that is, 2.0 is an integer, 2.1 is not.
If you want actualInt64 in there, you can use a heterogeneous vector with abstract eltype, for example, Real: