代码之家  ›  专栏  ›  技术社区  ›  Dulmina

使用扩散算子连接两个映射在芭蕾舞演员中产生错误

  •  0
  • Dulmina  · 技术社区  · 2 年前

    我曾尝试使用排列运算符连接两个映射,但它给出了一个错误。

    import ballerina/io;
    
    map<string> map1 = {
        "key1": "value1"
    };
    
    map<string> map2 = {
        "key2": "value2"
    };
    
    public function main() {
        map<string> map3 = {...map1, ...map2};
        io:println(map3);
    }
    

    错误消息: invalid usage of mapping constructor expression: multiple spread fields of inclusive mapping types are not allowed

    我在这里做错了什么?

    1 回复  |  直到 2 年前
        1
  •  0
  •   Dulmina    2 年前

    这是不允许的,因为在两个映射中可能指定了相同的字段。基本上,我们不能将扩散域与多个开放映射一起使用。即使是打开的,也必须从未键入可选字段 https://ballerina.io/learn/by-example/never-type/ 对于可以通过其他排列字段指定的所有字段,以确保不会出现重复。

    例如,以下内容将起作用。

    import ballerina/io;
    
    record {| 
        string key1;
    |} map1 = {
        key1: "value1"
    };
    
    record {|
        never key1?;
        string...;
    |} map2 = {
        "key2": "value2"
    };
    
    public function main() {
        map<string> map3 = {...map1, ...map2};
        io:println(map3);
    }
    
    推荐文章