使子模块中的导出函数可以在 Raku 的全局命名空间中访问

发布于 2025-01-11 22:20:06 字数 1103 浏览 0 评论 0 原文

我创建了一个名为 new 的最小工作模块。文件夹结构链接如下:

new
│   .gitignore
│   Changes
│   dist.ini
│   LICENSE
│   META6.json
│   README.md
│
├───lib
│   │   new.rakumod
│   │
│   ├───Desc
│   │       Mean.rakumod
│   │
│   └───Deviation
│           DeviationMean.rakumod
│
└───t
        01-basic.rakutest

我有两个函数,中的 Desc::Mean.rakumod 中的meanDeviation::DeviationMean.rakumod 模块中的deviation_from_mean lib。 这些是简单的函数,我不想为它们定义任何命名空间。因此,当我安装此模块并尝试通过 use new 使用此模块时,我希望能够访问这两个函数而不调用它们的子模块名称。

我想做的是(现在不起作用)

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);

而不是(有效)

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);

有办法做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自唱情﹋歌 2025-01-18 22:20:06

粗略地说&遵循docs,您可以将这些方法名称作为导出放入new命名空间(在new.rakumod中):

my package EXPORT::DEFAULT {
    OUR::mean := "Desc::Mean::mean";
    OUR::deviation_from_mean :=
         "Deviation::DeviationMean::deviation_from_mean";
}

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";
}
戒ㄋ 2025-01-18 22:20:06
# 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;

注意:

有关上述内容的解释,请参阅:

# 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 the new module must come before the unit module new; statement.

  • use further modules (eg Deviation::DeviationMean) as desired in the EXPORT sub to import those module's symbols into the new compunit; the return ::.pairs.grep(*.key ne '$_').Map; will then re-export all their symbols to whatever uses new.

For an explanation of the above see:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文