Callbacks?
On Thu, Mar 27, 2014 at 8:04 AM, andreas <[email protected]> wrote: > > Dear list, > > I'd like to be able to "pass" a function to a module, lets say > foo(x::Real). foo(x::Real) is used in the module and more methods are > added (which make use of foo(x::Real)). > > My idea is that the user loads the module and can then define foo(x::Real). > If the user later redefines this function, the package should use the new > foo(x::Real) for everything. > > > The small example below should make clear what I want. It works only > partly (on latest Windows pre-release), and not at all if the module is a > package (which is a concern for me). > > ## ----------- > module mymodule > > ## foo(x::Array) requires foo(x::Real) > function foo(x::Array) > [foo(i/2) for i in x]' > end > > ## bar() requires both, foo(x::Array) and foo(x::Real) > function bar(y) > a=length(y) > [foo(a) foo(y)] > end > > export bar > > end > ## ----------- > > using mymodule > > ## write foo(x::Real) in module > mymodule.foo(x::Real) = x^2 > > mymodule.foo(2) > ## -> 4 # as expected > bar([1:3]) > ## -> [9, 0.25, 1, 2.25] # as expected > > ## now change foo(x::Real) in module > mymodule.foo(x::Real) = x > > mymodule.foo(2) > ## -> 4 # changed as expected > bar([1:3]) > ## -> [9, 0.25, 1, 2.25] # bar() uses still the old foo(x::Real)! > > I have the feeling that this is a very bad design and would appreciate > every suggestions how I could get a similar functionality in a cleaner way. > > Thanks! > Andreas > >
