You should be able to do just
function g(g::G; start = :start) # there is no problem in having the same
name on a kwarg and the variable you pass, so calling as M.g(g2;start =
start) is OK
if (start == :start)
startFunc = Main.start
else
startFunc = start
end
startFunc::Function # don't know if this assertion helps the compiler to
optimize - if not, you might have performance problems with this approach
s = new_s(g)
startFunc(g, s) # changed "state" to "s" - if that wasn't a typo, ignore
this change
end
// T
On Monday, June 2, 2014 4:28:36 PM UTC+2, Robert Feldt wrote:
>
> Thanks to both of you. I have now rewritten it as:
>
> function g(g::G; startFunc = Main.start)
> s = new_s(g)
> startFunc(g, state)
> end
>
> and it works as expected. In the end I would actually want the user to
> supply the name of the startFunc as a symbol rather than caring about
> grabbing the function with that name. Any advice on how I can grab
> Main.start if I have funcname = :start? I guess I can do it through eval
> but that is slow?
>
> Regards,
>
> Robert
>
> Den måndagen den 2:e juni 2014 kl. 15:54:53 UTC+2 skrev Tomas Lycken:
>>
>> Is there a reason you don't just pass the `start` function as an argument
>> to the module function?
>>
>> function g(g::G, start::Function)
>> s = new_s(g)
>> println("start(", typeof(g), ", ", typeof(s), ")")
>> start(g, s)
>> end
>>
>> M.g(g2, (g::G2, s::M.DefaultS) -> 1)
>>
>> Since anonymous functions are quite slow in some contexts, you could
>> define the start method outside as you do, and then just pass it by name
>>
>> function mystart(g::G2, s::M.DefaultS)
>> 1
>> end
>>
>> M.g(g2, mystart)
>>
>> Does this solve your problem?
>>
>> On Monday, June 2, 2014 3:40:57 PM UTC+2, Robert Feldt wrote:
>>>
>>> Hi,
>>>
>>> I want to be able to call out from a function (defined within a module)
>>> to a method with a specific name but that is defined outside the module.
>>> Let me try to show the actual code to help clarify:
>>>
>>> module M
>>>
>>> abstract G
>>> s_type(g::G) = g.s_type
>>>
>>> function new_s(g::G)
>>> s = s_type(g)
>>> s(g)
>>> end
>>>
>>> # This is the function that should call out to methods named start
>>> # that are defined outside this module:
>>> function g(g::G)
>>> s = new_s(g)
>>> println("start(", typeof(g), ", ", typeof(s), ")")
>>> start(g, s)
>>> end
>>>
>>> abstract S
>>>
>>> type DefaultS <: S
>>> g
>>> DefaultS(g::G) = new(g)
>>> end
>>>
>>> end
>>>
>>> type G2 <: M.G
>>> s_type
>>>
>>> function G2()
>>> new(M.DefaultS)
>>> end
>>> end
>>>
>>> # This is the function I want to call out to from within the module M:
>>> function start(g::G2, s::M.DefaultS)
>>> 1
>>> end
>>>
>>> g2 = G2()
>>> s = M.DefaultS(g2)
>>>
>>> Now if I call start directly it does as expected:
>>>
>>> julia> start(g2, s)
>>> 1
>>>
>>> But if I call through the g function in the module it complains:
>>> ulia> M.g(g2)
>>> start(G2, DefaultS)
>>> ERROR: no method start(G2, DefaultS)
>>> in g at none:15
>>>
>>> despite:
>>>
>>> julia> methods(start)
>>> # 1 method for generic function "start":
>>> start(g::G2,s::DefaultS) at none:2
>>>
>>> What have I missed? Thanks for any help,
>>>
>>> Robert Feldt
>>>
>>