Solidity的自定义结构体深入详解

一.结构体定义

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

pragma solidity ^0.4.4;
 
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的声明中还使用了结构体。

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

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.。虽然如此,但我们仍然能在类型内用数组,映射来引用当前定义的类型,如变量mappingMemberOfOwn,arrayMemberOfOwn所示。

二.初始化
  下面我们来说说结构体的初始化。
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.命名初始化

  还可以使用类似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。另外,在初始化时,需要忽略映射类型[ignore],后面有具体说明。

3.结构体中映射的初始化
  由于映射是一种特殊的数据结构。

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.。

三.结构体的可见性
  关于可见性,当前只支持internal的,后续不排除放开这个限制。详见开发者christen的讨论:

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) { ... }".

继承中使用
  结构体由于是不对外可见的,所以你只可以在当前合约,或合约的子类中使用。包含自定义结构体的函数均需要声明为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),由于它包含了struct的S,所以不对外可见,需要标记为internal。你可以在当前类中使用它,如f1()所示,你还可以在子类中使用函数和结构体,如B合约的g()方法所示。

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

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){
    //你要实现的内容
  }
}

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

文章来源:http://me.tryblockchain.org/solidity-struct.html

猜你喜欢

转载自www.cnblogs.com/flyingeagle/p/10139898.html