how to properly nest modules in a package

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to properly nest modules in a package

ggggg
Lets say I'm making a package P.jl.  Inside P I define modules A, B and C all in separate files. I'd like to use C inside of A and B, and export a function from C from P. 

So right now B looks like
include("C.jl")
module B
using C
B body
end

And A looks like
include("C.jl")
module A
using C
A body
end

And P looks like
include("A.jl")
include
("B.jl")
include
("C.jl")
module P
using A,B,C
P body
end

I suspect that I'm doing it wrong for a few reasons.
1. I'm using two lines to get a module instead of one (include + using)
2. I've included C three times in P.
3. Autoreload.jl is complaining 

At the least it seems like I should remove "include("C.jl")" from P, which seems to help.  I'd appreciate some general guidance.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Iain Dunning
module P
  module C
    export foo
    foo() = 2
  end
  module A
    using ..C
  end
  module B
    using ..C
  end
end

Tested with
julia> P.A.C.foo()
2
  

On Tuesday, July 22, 2014 9:04:17 PM UTC-4, ggggg wrote:
Lets say I'm making a package P.jl.  Inside P I define modules A, B and C all in separate files. I'd like to use C inside of A and B, and export a function from C from P. 

So right now B looks like
include("C.jl")
module B
using C
B body
end

And A looks like
include("C.jl")
module A
using C
A body
end

And P looks like
include("A.jl")
include
("B.jl")
include
("C.jl")
module P
using A,B,C
P body
end

I suspect that I'm doing it wrong for a few reasons.
1. I'm using two lines to get a module instead of one (include + using)
2. I've included C three times in P.
3. Autoreload.jl is complaining 

At the least it seems like I should remove "include("C.jl")" from P, which seems to help.  I'd appreciate some general guidance.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Tim Holy
In reply to this post by ggggg
module P

include("C.jl")
include("A.jl")
include("B.jl")

end

B.jl:

module B
using ..C

# blah blah
end

--Tim

On Tuesday, July 22, 2014 06:04:17 PM ggggg wrote:

> Lets say I'm making a package P.jl.  Inside P I define modules A, B and C
> all in separate files. I'd like to use C inside of A and B, and export a
> function from C from P.
>
> So right now B looks like
> include("C.jl")
> module B
> using C
> B body
> end
>
> And A looks like
> include("C.jl")
> module A
> using C
> A body
> end
>
> And P looks like
> include("A.jl")
> include("B.jl")
> include("C.jl")
> module P
> using A,B,C
> P body
> end
>
> I suspect that I'm doing it wrong for a few reasons.
> 1. I'm using two lines to get a module instead of one (include + using)
> 2. I've included C three times in P.
> 3. Autoreload.jl is complaining
>
> At the least it seems like I should remove "include("C.jl")" from P, which
> seems to help.  I'd appreciate some general guidance.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Tim Holy
In reply to this post by Iain Dunning
Rats, I should check before posting.

--T

On Tuesday, July 22, 2014 06:09:40 PM Iain Dunning wrote:

> module P
>   module C
>     export foo
>     foo() = 2
>   end
>   module A
>     using ..C
>   end
>   module B
>     using ..C
>   end
> end
>
> Tested with
> julia> P.A.C.foo()
> 2
>
> On Tuesday, July 22, 2014 9:04:17 PM UTC-4, ggggg wrote:
> > Lets say I'm making a package P.jl.  Inside P I define modules A, B and C
> > all in separate files. I'd like to use C inside of A and B, and export a
> > function from C from P.
> >
> > So right now B looks like
> > include("C.jl")
> > module B
> > using C
> > B body
> > end
> >
> > And A looks like
> > include("C.jl")
> > module A
> > using C
> > A body
> > end
> >
> > And P looks like
> > include("A.jl")
> > include("B.jl")
> > include("C.jl")
> > module P
> > using A,B,C
> > P body
> > end
> >
> > I suspect that I'm doing it wrong for a few reasons.
> > 1. I'm using two lines to get a module instead of one (include + using)
> > 2. I've included C three times in P.
> > 3. Autoreload.jl is complaining
> >
> > At the least it seems like I should remove "include("C.jl")" from P, which
> > seems to help.  I'd appreciate some general guidance.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

ggggg
Ok I see how that works, I wasn't aware of the ..C syntax. That solves the problem asked about, but I'm left with another question. Take for example

module A
module B
foo
()=4
export foo
end
foo
()
end

That doesn't work, I get "ERROR: foo not defined" because foo is not actually in the A namespace. But if I add "using B" I get "ERROR: B not found". So how do I define B inside A, but also have A import the things that B exports?


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Mauro
It still needs relative imports with one dot:

julia> module A
       module B
       foo()=4
       export foo
       end
       using .B
       foo()
       end

Which is a bit odd.  Because at the REPL, which is in module Main, this
is not needed.  This both works:

julia> module T
       end

julia> using .T

julia> module U
       end

julia> using U

Does anyone know why this difference is?


On Wednesday, July 23, 2014 5:56:08 AM UTC+1, ggggg wrote:
Ok I see how that works, I wasn't aware of the ..C syntax. That solves the problem asked about, but I'm left with another question. Take for example

module A
module B
foo
()=4
export foo
end
foo
()
end

That doesn't work, I get "ERROR: foo not defined" because foo is not actually in the A namespace. But if I add "using B" I get "ERROR: B not found". So how do I define B inside A, but also have A import the things that B exports?


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Stefan Karpinski
Main is the root module so /T and ./T are the same thing in Main.


On Wed, Jul 23, 2014 at 3:36 AM, Mauro <[hidden email]> wrote:
It still needs relative imports with one dot:

julia> module A
       module B
       foo()=4
       export foo
       end
       using .B
       foo()
       end

Which is a bit odd.  Because at the REPL, which is in module Main, this
is not needed.  This both works:

julia> module T
       end

julia> using .T

julia> module U
       end

julia> using U

Does anyone know why this difference is?


On Wednesday, July 23, 2014 5:56:08 AM UTC+1, ggggg wrote:
Ok I see how that works, I wasn't aware of the ..C syntax. That solves the problem asked about, but I'm left with another question. Take for example

module A
module B
foo
()=4
export foo
end
foo
()
end

That doesn't work, I get "ERROR: foo not defined" because foo is not actually in the A namespace. But if I add "using B" I get "ERROR: B not found". So how do I define B inside A, but also have A import the things that B exports?



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Mauro
On Wed, 2014-07-23 at 16:29, Stefan Karpinski <[hidden email]> wrote:
> Main is the root module so /T and ./T are the same thing in Main.

but doesn't the same hold for any other "folder"?

   /MyMod/T and /MyMod/./T are the same thing

> On Wed, Jul 23, 2014 at 3:36 AM, Mauro <[hidden email]> wrote:
>
>> It still needs relative imports with one dot:
>>
>> julia> module A
>>        module B
>>        foo()=4
>>        export foo
>>        end
>>        using .B
>>        foo()
>>        end
>>
>> Which is a bit odd.  Because at the REPL, which is in module Main, this
>> is not needed.  This both works:
>>
>> julia> module T
>>        end
>>
>> julia> using .T
>>
>> julia> module U
>>        end
>>
>> julia> using U
>>
>> Does anyone know why this difference is?
>>
>>
>> On Wednesday, July 23, 2014 5:56:08 AM UTC+1, ggggg wrote:
>>>
>>> Ok I see how that works, I wasn't aware of the ..C syntax. That solves
>>> the problem asked about, but I'm left with another question. Take for
>>> example
>>>
>>> module A
>>> module B
>>> foo()=4
>>> export foo
>>> end
>>> foo()
>>> end
>>>
>>> That doesn't work, I get "ERROR: foo not defined" because foo is not
>>> actually in the A namespace. But if I add "using B" I get "ERROR: B not
>>> found". So how do I define B inside A, but also have A import the things
>>> that B exports?
>>>
>>>
>>>>

--
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Stefan Karpinski
Not quite. Note the initial /. When you do `using M` or `import M` it is like `cat /M` – it is relative to the root of the module system, not the current level of module. When you do `using .M` it is like `cat M` or `cat ./M` – it is relative to the current module. Otherwise when you wrote `using Gadfly` in your package, it would go looking for Gadfly inside your current module, which is unlikely to be where it is – unless you happen to have your own submodule called Gadfly, which is, of course, possible. If that's what you wanted, then you would use a relative import: `using .Gadfly`.


On Wed, Jul 23, 2014 at 8:34 AM, Mauro <[hidden email]> wrote:
On Wed, 2014-07-23 at 16:29, Stefan Karpinski <[hidden email]> wrote:
> Main is the root module so /T and ./T are the same thing in Main.

but doesn't the same hold for any other "folder"?

   /MyMod/T and /MyMod/./T are the same thing

> On Wed, Jul 23, 2014 at 3:36 AM, Mauro <[hidden email]> wrote:
>
>> It still needs relative imports with one dot:
>>
>> julia> module A
>>        module B
>>        foo()=4
>>        export foo
>>        end
>>        using .B
>>        foo()
>>        end
>>
>> Which is a bit odd.  Because at the REPL, which is in module Main, this
>> is not needed.  This both works:
>>
>> julia> module T
>>        end
>>
>> julia> using .T
>>
>> julia> module U
>>        end
>>
>> julia> using U
>>
>> Does anyone know why this difference is?
>>
>>
>> On Wednesday, July 23, 2014 5:56:08 AM UTC+1, ggggg wrote:
>>>
>>> Ok I see how that works, I wasn't aware of the ..C syntax. That solves
>>> the problem asked about, but I'm left with another question. Take for
>>> example
>>>
>>> module A
>>> module B
>>> foo()=4
>>> export foo
>>> end
>>> foo()
>>> end
>>>
>>> That doesn't work, I get "ERROR: foo not defined" because foo is not
>>> actually in the A namespace. But if I add "using B" I get "ERROR: B not
>>> found". So how do I define B inside A, but also have A import the things
>>> that B exports?
>>>
>>>
>>>>

--

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Mauro
got it, tnx

On Wed, 2014-07-23 at 16:51, Stefan Karpinski <[hidden email]> wrote:

> Not quite. Note the initial /. When you do `using M` or `import M` it is
> like `cat /M` – it is relative to the root of the module system, not the
> current level of module. When you do `using .M` it is like `cat M` or `cat
> ./M` – it is relative to the current module. Otherwise when you wrote
> `using Gadfly` in your package, it would go looking for Gadfly inside your
> current module, which is unlikely to be where it is – unless you happen to
> have your own submodule called Gadfly, which is, of course, possible. If
> that's what you wanted, then you would use a relative import: `using
> .Gadfly`.
>
>
> On Wed, Jul 23, 2014 at 8:34 AM, Mauro <[hidden email]> wrote:
>
>> On Wed, 2014-07-23 at 16:29, Stefan Karpinski <[hidden email]>
>> wrote:
>> > Main is the root module so /T and ./T are the same thing in Main.
>>
>> but doesn't the same hold for any other "folder"?
>>
>>    /MyMod/T and /MyMod/./T are the same thing
>>
>> > On Wed, Jul 23, 2014 at 3:36 AM, Mauro <[hidden email]> wrote:
>> >
>> >> It still needs relative imports with one dot:
>> >>
>> >> julia> module A
>> >>        module B
>> >>        foo()=4
>> >>        export foo
>> >>        end
>> >>        using .B
>> >>        foo()
>> >>        end
>> >>
>> >> Which is a bit odd.  Because at the REPL, which is in module Main, this
>> >> is not needed.  This both works:
>> >>
>> >> julia> module T
>> >>        end
>> >>
>> >> julia> using .T
>> >>
>> >> julia> module U
>> >>        end
>> >>
>> >> julia> using U
>> >>
>> >> Does anyone know why this difference is?
>> >>
>> >>
>> >> On Wednesday, July 23, 2014 5:56:08 AM UTC+1, ggggg wrote:
>> >>>
>> >>> Ok I see how that works, I wasn't aware of the ..C syntax. That solves
>> >>> the problem asked about, but I'm left with another question. Take for
>> >>> example
>> >>>
>> >>> module A
>> >>> module B
>> >>> foo()=4
>> >>> export foo
>> >>> end
>> >>> foo()
>> >>> end
>> >>>
>> >>> That doesn't work, I get "ERROR: foo not defined" because foo is not
>> >>> actually in the A namespace. But if I add "using B" I get "ERROR: B not
>> >>> found". So how do I define B inside A, but also have A import the
>> things
>> >>> that B exports?
>> >>>
>> >>>
>> >>>>
>>
>> --
>>

--
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

Stefan Karpinski
For what's it's worth, we originally looked relative first and then absolute, but it was a usability nightmare. This is more explicit and, once you get it, I think much simpler.


On Wed, Jul 23, 2014 at 9:06 AM, Mauro <[hidden email]> wrote:
got it, tnx

On Wed, 2014-07-23 at 16:51, Stefan Karpinski <[hidden email]> wrote:
> Not quite. Note the initial /. When you do `using M` or `import M` it is
> like `cat /M` – it is relative to the root of the module system, not the
> current level of module. When you do `using .M` it is like `cat M` or `cat
> ./M` – it is relative to the current module. Otherwise when you wrote
> `using Gadfly` in your package, it would go looking for Gadfly inside your
> current module, which is unlikely to be where it is – unless you happen to
> have your own submodule called Gadfly, which is, of course, possible. If
> that's what you wanted, then you would use a relative import: `using
> .Gadfly`.
>
>
> On Wed, Jul 23, 2014 at 8:34 AM, Mauro <[hidden email]> wrote:
>
>> On Wed, 2014-07-23 at 16:29, Stefan Karpinski <[hidden email]>
>> wrote:
>> > Main is the root module so /T and ./T are the same thing in Main.
>>
>> but doesn't the same hold for any other "folder"?
>>
>>    /MyMod/T and /MyMod/./T are the same thing
>>
>> > On Wed, Jul 23, 2014 at 3:36 AM, Mauro <[hidden email]> wrote:
>> >
>> >> It still needs relative imports with one dot:
>> >>
>> >> julia> module A
>> >>        module B
>> >>        foo()=4
>> >>        export foo
>> >>        end
>> >>        using .B
>> >>        foo()
>> >>        end
>> >>
>> >> Which is a bit odd.  Because at the REPL, which is in module Main, this
>> >> is not needed.  This both works:
>> >>
>> >> julia> module T
>> >>        end
>> >>
>> >> julia> using .T
>> >>
>> >> julia> module U
>> >>        end
>> >>
>> >> julia> using U
>> >>
>> >> Does anyone know why this difference is?
>> >>
>> >>
>> >> On Wednesday, July 23, 2014 5:56:08 AM UTC+1, ggggg wrote:
>> >>>
>> >>> Ok I see how that works, I wasn't aware of the ..C syntax. That solves
>> >>> the problem asked about, but I'm left with another question. Take for
>> >>> example
>> >>>
>> >>> module A
>> >>> module B
>> >>> foo()=4
>> >>> export foo
>> >>> end
>> >>> foo()
>> >>> end
>> >>>
>> >>> That doesn't work, I get "ERROR: foo not defined" because foo is not
>> >>> actually in the A namespace. But if I add "using B" I get "ERROR: B not
>> >>> found". So how do I define B inside A, but also have A import the
>> things
>> >>> that B exports?
>> >>>
>> >>>
>> >>>>
>>
>> --
>>

--

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to properly nest modules in a package

ggggg
It makes sense now, thanks.  

Loading...