結構體,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.
。雖然如此,但我們仍然能在類型內用數組,映射來引用當前定義的類型,如變量mappingMemberOfOwn
,arrayMemberOfOwn
所示。
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)
,由于它包含了struct
的S
,所以不對外可見,需要標記為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
版權所有,轉載注明出處
參考資料
-
http://ethereum.stackexchange.com/questions/15048/solidity-struct-initial-mapping-can-be-ignored ? ?
-
http://ethereum.stackexchange.com/questions/13365/mapping-member-isnt-initialized-when-creating-a-struct/13367#13367 ?
-
https://forum.ethereum.org/discussion/1994/does-solidity-allow-struct-return-types ?
-
http://ethereum.stackexchange.com/questions/11016/copy-a-struct-from-contract-a-into-a-struct-in-contract-b-using-contract-c/11020#11020 ?