Solidity的自定义结构体深入详解(十一)|入门系列

结构体,Solidity中的自定义类型。我们可以使用Solidity的关键字struct来进行自定义。结构体内可以包含字符串,整型等基本数据类型,以及数组,映射,结构体等复杂类型。数组,映射,结构体也支持自定义的结构体。我们来看一个自定义结构体的定义:

pragma solidity ^0.4.0;

contract SimpleStruct{
  //学生
  struct Student{
    string name;
    int num;
  }

  //班级
  struct Class{
    string clsName;
    //学生的列表
    Student[] students;
    mapping(string=>Student)index;
  }
}

在上面的代码中,我们定义了一个简单的结构体Student,它包含一些基本的数据类型。另外我们还定义了一个稍微复杂一点的结构体Class,它包含了其它结构体Student,以及数组,映射等类型。

数组类型的students和映射类型的index的声明中还使用了结构体。

1.1 结构体定义的限制

我们不能在结构中定义一个自己作为类型,这样限制原因是,自定义类型的大小不允许是无限的。我们来看看下述的代码:

pragma solidity ^0.4.0;

contract NoMemberOfOwn{
  struct A{
    //定义包含自己的会报错
    //Error: Recursive struct definition.
    //A a;
    
    mapping(int=>A) mappingMemberOfOwn;
    
    A[] arrayMemberOfOwn;
  }
}

在上面的代码中,我们尝试在A类型中定义一个A a;,将会报错Error: Recursive struct definition.。虽然如此,但我们仍然能在类型内用数组,映射来引用当前定义的类型,如变量mappingMemberOfOwnarrayMemberOfOwn所示。

2. 初始化

下面我们来说说结构体的初始化。

2.1 直接初始化

如果我们声明的自定义类型为A,我们可以使用A(变量1,变量2, ...)的方式来完成初始化。来看下面的代码:

pragma solidity ^0.4.0;

contract StructInitial{
  struct A{
    string name;
    mapping(address=>A) map;
    int age;
    string[] cources;
  }

  function init() returns (string, int, string){
    string[] memory cources = new string[](1);
    cources[0] = "Chemistry";

    //按顺序填值,初始化时,可以跳过映射类型
    A memory a = A("Jack", 23, cources);

    return (a.name, a.age, cources[0]);
  }
}

上面的代码中,我们按定义依次填入值,即可完成了初始化。需要注意的是,参数要与定义的数量匹配。当你填的参数与预计初始化的参数不一致时,会提示Error: Wrong argument count for function call: 2 arguments given but expected 3. Members that have to be skipped in memory: map。另外,在初始化时,需要忽略映射类型[1],后面有具体说明。

2.2 命名初始化

还可以使用类似JavaScript的命名参数初始化的方式,通过传入参数名和对应值的对象。这样做的好处在于可以不按定义的顺序传入值。我们来看看下面的例子:

pragma solidity ^0.4.0;

contract StructNamedInitial{
  struct Student{
    string name;
    mapping(address=>Student) map;
    int age;
    string[] cources;
  }

  function init() returns (string, int, string){
    string[] memory crs = new string[](1);
    crs[0] = "Chemistry";

    //按命名参数的方式进行初始化
    Student memory s = Student({
        age : 10,
        name : "Jack",
        cources: crs
      });

    return (s.name, s.age, s.cources[0]);
  }
}

上面的例子中,通过在参数对象中,指定键为对应的参数名,值为你想要初化的值,我们即完成了初始化。同样需要注意的是,参数要与定义的个数一致,否则会报类似这样的错误Error: Wrong argument count for function call: 2 arguments given but expected 3. Members that have to be skipped in memory: map。另外,在初始化时,需要忽略映射类型[1],后面有具体说明。

2.3 结构体中映射的初始化

由于映射是一种特殊的数据结构[2]

Mappings can be seen as hashtables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value. The similarity ends here, though: The key data is not actually stored in a mapping, only its keccak256 hash used to look up the value.

Because of this, mappings do not have a length or a concept of a key or value being “set”.

可以你可能只能在storage变量中使用它。

pragma solidity ^0.4.0;

contract StructMappingInitial{
  struct A{
    string name;
    mapping(address=>A) map;
    int age;
    string[] cources;
  }

  //分配映射的空间
  A storageVar;

  function init() returns (string, int, string){
    string[] memory cources = new string[](1);
    cources[0] = "Chemistry";

    A memory a = A("Jack", 23, cources);

    storageVar = a;
    storageVar.map[msg.sender] = a;

    return (a.name, a.age, cources[0]);
  }
}

上面的例子中,我们定义的了一个storage的状态变量storageVar,完成了映射类型的存储空间分配。然后我们就能对映射类型赋值了。

如果你尝试对memory的映射类型赋值,会报错Error: Member "map" is not available in struct StructMappingInitial.A memory outside of storage.

3. 结构体的可见性

关于可见性,当前只支持internal的,后续不排除放开这个限制。详见开发者christen的讨论[3]

Since all variables are pre-initialised in Solidity, so are return values of struct type (with their members being initialised recursively). This means if you use
function f() internal returns (Record r) { ... } you could also just assign the members of r individually.
The struct itself resides in memory and the function returns a reference to this point in memory. This means that in the case of "return records[recordID]", the storage-struct is first copied to memory and then the function returns a reference to this place in memory. If you would like to return the storage reference itself, you have to use "function ... returns (Record storage) { ... }".

3.1 继承中使用

结构体由于是不对外可见的,所以你只可以在当前合约,或合约的子类中使用。包含自定义结构体的函数均需要声明为internal的。

pragma solidity ^0.4.0;

contract A{
  struct S{
    string para1;
    int para2;
  }

  function f(S s) internal{
      //...
  }

  function f1(){
    //当前类中使用结构体
    S memory s = S("Test", 10);
    f(s);
  }
}

contract B is A{
  function g(){
      //字类中使用结构体
      S memory s = S("Test", 10);

      //调用父类方法
      f(s);
  }
}

在上面的代码中,我们声明了f(S s),由于它包含了structS,所以不对外可见,需要标记为internal。你可以在当前类中使用它,如f1()所示,你还可以在子类中使用函数和结构体,如B合约的g()方法所示。

4. 跨合约的临时解决方案

结构体,由于是动态内容。当前不支持在多个合约间互用,目前一种临时的方案如下[4]:。

pragma solidity ^0.4.0;

contract StructAcrossInitial{
  struct A{
    string para1;
    int para2;
  }

  function call(B b){
    A memory a = A("Test", 10);

    b.g(a.para1, a.para2);
  }
}

contract B{
  function g(string para1, int para2){
    //你要实现的内容
  }
}

在上面的例子中,我们手动将要返回的结构体拆解为基本类型进行了返回。

关于作者

专注基于以太坊(Ethereum)的相关区块链(Blockchain)技术,了解以太坊,Solidity,Truffle,web3.js。

个人博客: http://me.tryblockchain.org
版权所有,转载注明出处

参考资料


  1. http://ethereum.stackexchange.com/questions/15048/solidity-struct-initial-mapping-can-be-ignored

  2. http://ethereum.stackexchange.com/questions/13365/mapping-member-isnt-initialized-when-creating-a-struct/13367#13367

  3. https://forum.ethereum.org/discussion/1994/does-solidity-allow-struct-return-types

  4. http://ethereum.stackexchange.com/questions/11016/copy-a-struct-from-contract-a-into-a-struct-in-contract-b-using-contract-c/11020#11020

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容