Inside B you have to do:
B.f(x::B.orange) = println("I am orange")
If you can be bothered you can read through some recent, long
discussions on the merits of this approach.
(BTW, I think in Julia 0.4, your example should give a better error.)
On Sat, 2015-05-23 at 06:43, Gabriel Goh <[email protected]> wrote:
> Hey all,
>
> I'm wondering if you guys could educate me on the correct way to overload
> functions in modules. The code here doesn't work:
>
> # ******************************************************
>
> module A
>
> type apple
> end
>
> export apple,f
>
> f(x::A.apple) = println("I am apple");
>
> end
>
> # ******************************************************
>
> module B
>
> type orange
> end
>
> export orange, f
>
> f(x::B.orange) = println("I am orange");
>
> end
>
> using A
> using B
>
> f(apple())
> f(orange())
>
> I expect julia to print "I am apple" and "I am orange", but instead I get
>
> ERROR: `f` has no method matching f(::apple)
>
> Any clues?