Private functions in the modules

337 views
Skip to first unread message

Devendra Ghate

unread,
Feb 27, 2015, 3:11:35 AM2/27/15
to julia...@googlegroups.com
Hello,
Consider the following example from the julia manual page on `modules`.

~~~
module MyModule

export x, y

x() = "x"
y() = "y"
p() = "p"

end
~~~

Load the module by any method makes function `MyModule.p()` available
in the main workspace.

1. `using MyModule`
2. `using MyModule.x`
3. `import MyModule`
4. `import MyModule.x`

I would expect `p` (as a private function) to be not available for
execution outside module. Manual page also mentions that this should
be the case.

I am using Julia 0.3.3. May be I need to upgrade.

Cheers,
Devendra.

Steven Sagaert

unread,
Feb 27, 2015, 5:28:04 AM2/27/15
to julia...@googlegroups.com
It's not a bug it's a fature ;)
I found this odd also when I was new to julia and complained about it. I wanted strict private visibillity like in C++/Java/C#,.... but the julia team does not want this. The only thing export does is that you can call the function without the module prefix.

Devendra Ghate

unread,
Feb 27, 2015, 5:50:53 AM2/27/15
to julia...@googlegroups.com
Ok. Then the manual page on modules should be rewritten. For example,
this line from the manual needs to be changed.

Import also differs from `using` in that functions must be imported
using `import` to be extended with new methods.

I only imported MyModule.x and was able to overload MyModule.y
(exported function) as well as MyModule.p (not exported) functions.

As far as I understand, there is no difference between an exported
function and non exported function.

I can make an effort to make changes to the manual, but I suspect that
it will be riddled with errors as I have only started with Julia.

Devendra
--
Cheers,
Devendra.

Jiahao Chen

unread,
Feb 27, 2015, 10:46:37 PM2/27/15
to julia...@googlegroups.com

On Fri, Feb 27, 2015 at 5:50 AM, Devendra Ghate <devendr...@gmail.com> wrote:
I only imported MyModule.x and was able to overload MyModule.y
(exported function) as well as MyModule.p (not exported) functions.

As far as I understand, there is no difference between an exported
function and non exported function.

Are you sure you cleared the namespace between trying the four versions of using/import MyModule/.x ?

If you only import MyModule.x, the method definitions for MyModule.p and MyModule.y are never loaded. You can define a new y function but that is not the same thing as overloading MyModule.y.

julia> import MyModule.x; x()
"x"

julia> x(a)=1
x (generic function with 2 methods)

julia> y() #MyModule.y not available
ERROR: UndefVarError: y not defined

julia> y()=2 #Define a new function
y (generic function with 1 method)

Jiahao Chen
Staff Research Scientist
MIT Computer Science and Artificial Intelligence Laboratory

Devendra Ghate

unread,
Feb 28, 2015, 9:48:13 AM2/28/15
to julia...@googlegroups.com
On Fri, Feb 27, 2015 at 10:46:14PM -0500, Jiahao Chen wrote:
>On Fri, Feb 27, 2015 at 5:50 AM, Devendra Ghate <devendr...@gmail.com>
>wrote:
>
>> I only imported MyModule.x and was able to overload MyModule.y
>> (exported function) as well as MyModule.p (not exported) functions.
>>
>> As far as I understand, there is no difference between an exported
>> function and non exported function.
>>
>
>Are you sure you cleared the namespace between trying the four versions of
>using/import MyModule/.x ?
>
>If you only import MyModule.x, the method definitions for MyModule.p and
>MyModule.y are never loaded. You can define a new y function but that is
>not the same thing as overloading MyModule.y.
>
>julia> import MyModule.x; x()
>"x"
>
>julia> x(a)=1
>x (generic function with 2 methods)
>
>julia> y() #MyModule.y not available
>ERROR: UndefVarError: y not defined
>
>julia> y()=2 #Define a new function
>y (generic function with 1 method)

Hello Jiahao,

I thought that after importing only a function from a module, rest of
the functions (that are exported by the module) should be inaccessible. Also,
functions not exported will remain inaccessible always.

~~~
julia> versioninfo()
Julia Version 0.3.3
Commit b24213b (2014-11-23 20:19 UTC)
Platform Info:
System: Linux (x86_64-unknown-linux-gnu)
CPU: Intel(R) Core(TM) i3 CPU M 380 @ 2.53GHz
WORD_SIZE: 64
BLAS: libblas
LAPACK: liblapack
LIBM: libm
LLVM: libLLVM-3.3

julia> import MyModule.x; x()
"x"

julia> p() # Not available in the current namespace, but
ERROR: p not defined

julia> MyModule.p() # a private function (that is not exported by MyModule) is available.
"p"

julia> MyModule.p(a::Int64)=1; MyModule.p(3) # Overloading...
1

julia> y() # Not available in the current namespace
ERROR: y not defined

julia> MyModule.y() # But still available by referencing. This seems to be a feature which was counter intuitive for me.
"y"
~~~

So after reading the manual, I got the wrong impression. Perhaps this snippet might be
added to the manual, and may be useful for complete beginners.

Cheers,
Devendra
Reply all
Reply to author
Forward
0 new messages