Hi Chiyuan, The problem is that your myfunc outside the module is shadowing the name myfunc, rather than extending it. In order to avoid accidental naming collisions, you have to tell Julia when you're intentionally extending a function from another module with new methods.
You have two syntax options to indicate that you're extending Foo's myfunc: 1. Add the line `import Foo.myfunc`. This will mean any `myfunc` defined in the current module will extend Foo's myfunc. If you export myfunc from Foo, then I believe that you don't need the `Foo.` part. 2. Declare the function as `Foo.myfunc`. By which I mean: function Foo.myfunc(a::TSub) @printf "override function is called\n" end Hope this helps, Leah On Sat, Jan 11, 2014 at 4:48 AM, Chiyuan Zhang <[email protected]> wrote: > Hi all, > > I'm trying to implement something that allows user to derive a new type > from my abstract type and also add new behavior to the methods operating on > those new types. For example, the following code snippet: > > > ---------------------------------------------x------------------------------------- > module Foo > > abstract TBase > > function myfunc(a::TBase) > @printf "base function is called\n" > end > > function invoke(a::TBase) > myfunc(a) > end > > export TBase > #export myfunc > export invoke > > end > > > > using Foo > > type TSub <: TBase > end > > function myfunc(a::TSub) > @printf "override function is called\n" > end > > foo = TSub() > invoke(foo) > ---------------------------------x--------------------------------- > > my intention is that when user call "invoke" with a subtype, the "myfunc" > defined for this subtype should be called. Everything works fine if I > didn't put my internal stuff inside a module. However, after I put my stuff > in a module, it doesn't work: only the original base method is called. I > have tried to export "myfunc" or not, but it doesn't change. > > Can anyone point me out how should I work around this or what is the > correct way of doing this? I suppose this is a quite common scenario as in > traditional OOP, we allow the user to provide derived type with customized > behavior on that type. > > Best, > Chiyuan >
