Re: [julia-users] Macro expansion for ccall with tuple argument

2016-04-07 Thread Yichao Yu
> and I programmed it as
> julia> macro do_ccall(f,args...)
> :(ccall(f,Int,$(ntuple(i->Int,length(args))),args...)) end

I belive the arguments should be a tuple expression which you can
generate with Expr(:tuple, [Int for i in 1:length(args)]...)

> julia> macroexpand(:(@do_ccall(f,1,2,3)))
> :(ccall(f,Int,(Int64,Int64,Int64),args...))
>

In general you can use `Meta.show_sexpr` (or `dump`/`xdump`) to see
more precisely what the expression is.


[julia-users] Macro expansion for ccall with tuple argument

2016-04-07 Thread Laurent Bartholdi
Hi,
I'm trying to understand macro expansion, and am a bit stuck. I would like, 
in distilled form, to have a macro "@do_ccall" which calls its argument 
with Int type. Thus I would like

julia> macroexpand(:(@do_ccall(f,1,2,3)))
:(ccall(f,Int,(Int64,Int64,Int64),args...))

and I programmed it as
julia> macro do_ccall(f,args...) 
:(ccall(f,Int,$(ntuple(i->Int,length(args))),args...)) end
@do_ccall (macro with 1 method)

Now the tuple argument just doesn't seem to get interpreted properly:

julia> @do_ccall(C_NULL,1,2,3)
ERROR: syntax: ccall argument types must be a tuple; try "(T,)"
 in eval(::Module, ::Any) at ./boot.jl:237

(note: calling the routine at C_NULL would have crashed Julia; it's just 
for demonstration purposes)

Thanks in advance! Laurent