如何导出一个method

pub type A Int
pub type B Int
pub fn A::from_int(x : Int) -> A {
  A(x)
}
pub fn B::from_int(x : Int) -> B {
  B(x)
}

我想要只导出 A::from_int ,应该怎么写?

如果 mod.pkg.json 里写成

{
    "link": {
        "js": {
            "exports": [
                "from_int"
            ]
        }
    }
}

然后运行 moon build --target=js ,得到的 project1.js 就是

function username$project1$$A$from_int(x) {
  return x;
}
function username$project1$$B$from_int(x) {
  return x;
}
export { 
  username$project1$$A$from_int as from_int, 
  username$project1$$B$from_int as from_int 
}

两个 from_int 都被导出了,还用了同一个名字。而如果把 mod.pkg.json 中的 "from_int" 改成 "A::from_int" ,得到的 project1.js 就是空文件

目前只支持导出普通函数。

1 个赞