微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ballerina - Swan Lake Beta 1 - 如何在模块中调用函数?

如何解决Ballerina - Swan Lake Beta 1 - 如何在模块中调用函数?

我正在使用芭蕾舞演员天鹅湖测试版 bal dist 列表 => * [slbeta1] Beta 1

我有一个bal new ack 创建的包“ack” 我在那个包“phooey”中有一个模块,由 bal add phooey

创建

确认 - main.bal

import ballerina/io;

public function main() {
    io:println(sayWhat());
}

在 phooey 模块中 - phooey.bal

public function sayWhat() returns string{
    return "Phooey!";
}

Ballerina.toml 是认值:

[build-options]
observabilityIncluded = true

我无法编译这个包。对 sayWhat()调用不可见

ERROR [main.bal:(4:16,4:25)] undefined function 'sayWhat'
error: compilation contains errors

尽管多次阅读文档,尝试了许多不同的方法,我还是找不到让模块代码认模块/包代码可见的方法。我尝试了多种方法来通过不同格式的 import 语句使其可见。他们不合规。我尝试通过模块引用和 package.module 引用来引用该函数。没有编译。

如何使模块代码认模块/包代码可见?

更新:在下面尝试了 Sameera 的建议。这没用。结果是:

ERROR [main.bal:(2:1,2:15)] cannot resolve module 'phooey'
ERROR [main.bal:(5:16,5:25)] undefined function 'sayWhat'
error: compilation contains errors

Update2:有效的是:

import ack.phooey;

尽管我在实际项目中遇到了挑战,该项目具有除 phooey.bal 等效项以外的功能

Update3 这似乎是根据我的用户名创建了一个包。将 Ballerina.toml 编辑为:

[package]
org = "doh"
name = "ack"
version = "0.1.0"

[build-options]
observabilityIncluded = true

根据要求 - 项目结构

blah/blah/ack
│   .gitignore
│   Ballerina.toml
│   main.bal
│
├───modules
│   └───phooey
│           phooey.bal
│
└───target

解决方法

要引用另一个模块中的符号,您需要使用模块前缀。有关详细信息,请参阅以下链接。 https://ballerina.io/learn/by-example/programs-and-modules https://ballerina.io/learn/user-guide/ballerina-packages/dependencies/

尝试按如下方式更新您的 main.bal

import ballerina/io;
// By default last part of the module name becomes to prefix
// Since this module is in the same package,you don't have to use the organization name.
import ack.phooey;

public function main() {
    io:println(phooey:sayWhat());
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。