module vs package, module and package

Viewed 248

I'm looking for some cleanup of my knowledge. Within a project with complex module structure I'd like to keep the structure clean by building up structured namespace tree. Say, something like:

App
  Config
    Key
    Node
    Param
    Type
      MyType

Every entry under App::Config shall be contained in its own file. Always typing things like App::Config::Key is a time waste. is export doesn't have a parameter to declare the name which is to be exported. So, I finally came to the following solution:

Config.pm6:

unit module App::Config:ver<0.0.1>;
...

Key.pm6:

unit package App::Config;

class Key is export {
    ...
}

And it works as I want it:

use App::Config::Key;

say Key.^name; # App::Config::Key

The only question remains: a there any caveats? Any hidden side effects to know about?

1 Answers

Far as I can tell, the only problem seems to be you will only be able to recursively declare modules that way. Only one, at the top level, will be possible. See this (simplified) example:

lib/Packr/U/Packd.rakumod:

unit package Packr::U;

our class Pack'd is export {}

lib/Packr/U.rakumod:

unit module Packr::U;

lib/Packr/Packd.rakumod:

unit package Packr;

our class Pack'd is export {}

our class B::Pack'd is export {}

lib/Packr.rakumod:

unit module Packr::U;

And then the main program:

use Packr;
use Packr::U;

for (Packr::Pack'd Packr::B::Pack'd Packr::U::Pack'd ) -> \P {
    say P.new().raku;
}

This fails in the last iterated class:

Could not find symbol '&Pack'd' in 'Packr::U'
Related