从本地包导入芭蕾舞女演员

Ballerina import from local package

我有一个芭蕾舞项目结构如下

/  <- project root
|
 - my.foo <- first package
      |
       - FooFunctions.bal <- Some .bal file
|
 - my.bar <- second package
      |
       - BarFunctions.bal <- Another .bal file

注意包名称空间的使用方式。他们在中间有 .。现在假设我有以下简单的 BarFunction.bal

public function someName() returns int {
  return 10;
}

如何引用和使用 FooFunctions.bal 中的 someName

关于打包的官方文档可以从这里找到link

简单来说[从 Ballerina 0.982 版本开始],您可以在 my.foo[=33= 中导入 my.bar 包] 包的任何 .bal 文件如下,

import ballerina/io;
import <org-name>/my.bar;

public function main(string... args) {
    io:println(bar:someName());
}

从项目的根级别 Ballerina.toml 文件的 org-name 值替换 <org-name>。并注意 bar 如何用于引用来自 my.bar 包的函数。这在 Ballerina 文档中突出显示如下,

Identifiers are either derived or explicit. The default identifier is either the package name, or if the package name has dots . include, then the last word after the last dot.

此外,您可以为您导入的包选择一个标识符。例如,我可以使用以下语法将 <org-name>/my.bar 识别为 barimport

import ballerina/io;
import <org-name>/my.bar as barimport;  # Now we refer import as barimport

public function main(string... args) {
    io:println(barimport:someName());
}

我认为 "BarFunctions.bal" 和 "BarFunction.bal" 是一样的。如果是这样,您可以将 "my.foo" 模块导入 "BarFunctions.bal" bal 文件,如下所示:

import my.foo;

您不需要模块的组织名称,因为它们来自同一个项目。

由于'my.foo' 和'my.bar' 模块都来自同一个项目,因此在定义导入语句时不需要提供组织名称。

根据您的情况,如果您想使用 'my.foo' 中 'my.bar' 模块中的 'someName()' 函数,您只需在 'FooFunctions.bal' 中执行以下操作:

import my.bar; 

public function main() {   
  // i will have the value returned from 'someName()' function
  int i = bar:someName(); 
}