My gut reaction is that you don't want to use a macro here. Can you use a
parametric definition:
f{T}(vectype:T) = <do something useful with the T>
or can you just use multiple dispatch:
f{T<:FloatingPoint}(v::Vector{T}) = <something for floats>
f{T<:Integer}(v::Vector{T}) = <something for ints>
What's your use case?
But to answer your question... I think this should work:
julia> macro customFun(vectype::Expr, name::Symbol)
quote
function $(esc(name))(v::$(esc(vectype)))
println(typeof(v))
end
end
end
julia> @customFun Vector{Int} f
f (generic function with 1 method)
julia> f(Int[])
Array{Int64,1}
On Wednesday, June 17, 2015 at 2:13:41 PM UTC-4, Ben Ward wrote:
>
> Hi, I want to create a macro with which I can create a function with a
> custom bit of code:
>
> In the repl I can do a toy example:
>
>
> *name = :hi*
>
>
> *vectype = Vector{Int}*
>
>
> *quote** function ($name)(v::$vectype)*
>
> *println("hi")*
>
> *end*
>
> *end*
>
> However if I try to put this in a macro and use it I get an error:
>
> *macro customFun(vectype::DataType, name::Symbol)*
>
>
> * quote** function ($name)(v::$vectype)*
>
> * println("hi World!")*
>
> * end*
>
> * end*
>
>
>
> *end*
>
> *@customFun(Vector{Int}, :hi)*
>
> What am I doing wrong? I'd like to use macro arguments to provide a
> function's name, and the datatype of the argument. I haven't used macros to
> define functions more complex than simple one liners.
>
> Thanks,
> Ben.
>