共享函数有非共享 return 类型

Shared function has non-shared return type

get 方法返回 Item 时出现错误 Shared function has non-shared return type

如何制作物品 shared 或者是否有更好的退货方式 properties/object

public type Item = {
    id: Nat;
    var name: Text;
};

actor Maas {
    var items: [Item] = [];

    ...

    public shared query func get(id: Nat) : async Item {
        return businesses[id - 1];
    };
};

我刚开始接触 Motoko,但我深入研究了一些文档。

据我了解,您需要将您的类型设置为共享类型 (https://smartcontracts.org/docs/language-guide/language-manual.html#sharability)

A type T is shared if it is:

- an object type where all fields are immutable and have shared type, or

- a variant type where all tags have shared type

因为 var 声明了可变变量 (https://smartcontracts.org/docs/language-guide/mutable-state.html#_immutable_versus_mutable_variables) 类型变为非共享类型。

所有需要做的就是从类型 Item

中删除 var
public type Item = {
    id: Nat;
    name: Text;
};

actor Maas {
    var items: [Item] = [];

    ...

    public shared query func get(id: Nat) : async Item {
        return businesses[id - 1];
    };
};