On Sunday, February 8, 2015 at 5:45:55 PM UTC-8, Dominique Orban wrote:
>
> I'm writing a function that takes optional arguments. Many of them must be 
> arrays of a certain size. I'd like to perform a quick check of their size 
> at the top of the function and thought it would be a good use case for meta 
> programming, but I can't seem to get the syntax right. Here's an example 
> that hopefully illustrates what I'm trying to do:
>
> function test(a; x=zeros(5), y=zeros(5))
>   for arg in {:x, :y}
>     if length(eval(arg)) != 5  # ERROR: x not defined
>       error(@eval $(string(arg, " must have length 5")))
>     end
>   end
>   # ...
> end
>

You don't need or want eval here.  I would just do

for arg in (x,y)
     length(arg) == 5 || error("optional arguments must have length 5")
end
 

Reply via email to