That's why I tried *@eval $(arg)*. In your example, if you change
*eval(:myarg)* to *@eval $(myarg)*, the call prints *10*.
On Sunday, February 8, 2015 at 10:57:43 PM UTC-5, Josh Langsfeld wrote:
>
> At least for why is doesn't work, the eval runs in the scope of the
> current module, not in the local scope of the function:
>
> julia> myarg = "a string"
> "a string"
>
>
> julia> f(myarg::Int) = println(eval(:myarg))
> f (generic function with 1 method)
>
>
> julia> f(10)
> a string
>
> I don't know if there is a way to get the value associated with a symbol
> inside a function.
>
> On Sunday, February 8, 2015 at 8:45:55 PM UTC-5, 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
>>
>> I tried *@eval $(arg)* in the argument to *length*, but without success.
>> Line 3 always causes "ERROR: x not defined" to be raised.
>>
>> Thanks!
>>
>