3
votes

Lets say I have a Julia module, MyModule, that defines a function called conv() for 1D convolution. I would like to be able to call this function via MyModule.conv() in a file that imports or uses the file. Example:

import MyModule.conv
MyModule.conv()

However, I cannot get this syntax to work; Julia still calls Base.conv() instead of MyModule.conv(). I have tried all the different styles of using and import but cannot get this syntax to work.

Is this functionality available in Julia? I feel like I have seen this be implemented in other Julia packages but cannot find an example that works.

EDIT

Current setup is as follows; there is no reference to conv() anywhere in ModA outside of the definition.

module ModA

function conv(a, b)
    println("ModA.conv")
end

end 

Then, in another file,

import ModA
conv(x, y) #still calls Base.conv()

SOLVED
This was entirely my fault. The import wasn't working because of an incorrect LOAD_PATH calling a different version of the file than I thought was being called (finding the first one in the LOAD_PATH, which was not the one that I was editing). Entirely my fault...

1
Write your module as you have in the edit. Then in another file (or at the REPL), type using ModA, then ModA.conv(randn(2), randn(2)). Is that not the behaviour you are after?Colin T Bowers
in ModA, you should import Base.conv.Gnimuc
If you import Base.conv in ModA then you will be overloading methods onto it, and there will be only one conv function at play. As I understand it, the intent here was to keep two separate functions.Toivo Henningsson

1 Answers

3
votes

Do you mean something like this?

module ModA
   conv() = println("Calling ModA.conv")
end

module ModB  # Import just conv from ModA
   using ..ModA.conv # Could have used import ..ModA.conv instead
   conv()            # if we want to be able to extend ModA.conv
end

module ModC  # Import ModA and use qualified access to access ModA.conv
   import ..ModA
   ModA.conv()
end

You must make sure not to make any reference to conv inside ModA before you have defined your own function, or it will already have looked up Base.conv and associated the name conv with that.