I created a minimal working module called new
. The folder structure, link here is as follows:
new
│ .gitignore
│ Changes
│ dist.ini
│ LICENSE
│ META6.json
│ README.md
│
├───lib
│ │ new.rakumod
│ │
│ ├───Desc
│ │ Mean.rakumod
│ │
│ └───Deviation
│ DeviationMean.rakumod
│
└───t
01-basic.rakutest
I have two functions, mean
in Desc::Mean.rakumod
and deviation_from_mean
in Deviation::DeviationMean.rakumod
modules in lib
.
These are simple functions, I don't want to have any namespace defined for them. So when I install this module, and try to use this module with use new
, I want to be able to access these two functions without calling their sub-module names.
What I want to do is (which does not work now)
use new;
my @test1 = [6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4];
say mean(@test1);
say deviation_from_mean(@test1);
instead of (which works)
use new;
use Desc::Mean;
use Deviation::DeviationMean;
my @test1 = [6,6,4,6,8,6,8,4,4,6,6,8,8,8,8,8,8,4,4,4,4,8,8,8,8,4,4,4,8,6,8,4];
say mean(@test1);
say deviation_from_mean(@test1);
Is there a way to do this?
CodePudding user response:
Roughly speaking & following the docs you can put these method names as exports into the new
namespace like this (in new.rakumod):
my package EXPORT::DEFAULT {
OUR::mean := "Desc::Mean::mean";
OUR::deviation_from_mean :=
"Deviation::DeviationMean::deviation_from_mean";
}
CodePudding user response:
# main.raku
use lib 'lib';
use new;
say mean; # 42
# Desc::Mean
unit module Desc::Mean;
sub mean is export { 42 }
# new
sub EXPORT {
{
use Desc::Mean;
return ::.pairs.grep(*.key ne '$_').Map;
}
}
unit module new;
Notes:
The
sub EXPORT { ... }
in thenew
module must come before theunit module new;
statement.use
further modules (egDeviation::DeviationMean
) as desired in theEXPORT
sub to import those module's symbols into thenew
compunit; thereturn ::.pairs.grep(*.key ne '$_').Map;
will then re-export all their symbols to whateveruse
snew
.
For an explanation of the above see:
jnthn's answer to Use Haskell like Prelude modules in a module.
The "Importing symbols into a package and then re-exporting them" section of my answer to Separating operator definitions for a class to other files and using them.