Hello colleague,

i'm not an expert on macros, but if you try to learn correct macroexpansion 
you should use a different target than ccall. ccall looks like a regular 
function call but has special handling (you could see this in lowered code) 
and that's the reason the arguments are expected to be constants. In my own 
examples to build ccalls from paramters i build the expressions manually 
and eval-ed.

On Thursday, April 7, 2016 at 1:29:06 PM UTC+2, Laurent Bartholdi wrote:
>
> 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
>

Reply via email to