Thanks for the comments. With a bit more digging I was able to figure out
how to pass a module in which to look for the function, this finds the
correct function and avoids eval.
module Bar
bar(s::String, args...;m::Module=Main) = getfield(m,symbol(s))(args...)
export bar
end
using Bar
foo(s::String, args...;m::Module=Main) = getfield(m,symbol(s))(args...)
baz(x...) = +(x...)
baz(4,5,6)
foo("baz",4,5,6)
bar("baz",4,5,6)
I want to be able to write a Step object to disk, and recreate it with the
correction function later. I see now how do that with func::Function, but
I hadn't previously seen how to do that so I went with a String.
On Tuesday, July 29, 2014 3:20:44 PM UTC-6, Leah Hanson wrote:
>
> In general, you should avoid using `eval`. Is there a reason you don't
> want to pass in a function and make it `func::Function` in your type?
>
> Your `eval` inside module `Bar` is evaluating the code inside the scope of
> `Bar`, which is what's causing your problem.
>
> -- Leah
>
>
> On Tue, Jul 29, 2014 at 3:58 PM, ggggg <[email protected] <javascript:>>
> wrote:
>
>> I'm trying to write a small module that allows one to define a Step that
>> describes operations on an HDF5 file.
>>
>> immutable Step
>> func::String
>> a_ins::(String...) #attribute inputs
>> d_ins::(String...) #dataset inputs
>> a_outs::(String...) #attribute outputs
>> d_outs::(String...) #dataset outputs
>> end
>>
>> The idea is to gather up the inputs specified by a_ins and d_ins, pass
>> them to the function specified by func, and place the outputs in HDF5
>> datasets and attributes as specified by a_outs and d_outs. The issues I'm
>> having is finding the correct function given that it is defined in some
>> other Module. A minimal example is given by
>>
>> module Bar
>> bar(s::String, args...) = eval(parse(s))(args...)
>> export bar
>> end
>> using Bar
>> foo(s::String, args...) = eval(parse(s))(args...)
>>
>> baz(x...) = +(x...)
>>
>> baz(4,5,6)
>> foo("baz",4,5,6)
>> bar("baz",4,5,6) # ERROR: baz not defined
>>
>> One path I can see is that when I create the Step I could pass an actual
>> function to the constructor. If I knew how to access the fully qualified
>> name of the function, I could record that instead of just the name. I'm
>> not sure if that is possible. Any ideas on how I should approach this?
>>
>> Also I probably shouldn't be using eval(parse(s)) since that opens up the
>> opportunity for arbitrary code execution.
>>
>
>