hyperdelia
hyperdelia

Reputation: 1145

Use module function instead of Base function Julia

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...

Upvotes: 3

Views: 551

Answers (1)

Toivo Henningsson
Toivo Henningsson

Reputation: 2699

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.

Upvotes: 3

Related Questions