結(jié)構(gòu)體,Solidity中的自定義類型烤礁。我們可以使用Solidity的關(guān)鍵字struct
來進行自定義讼积。結(jié)構(gòu)體內(nèi)可以包含字符串,整型等基本數(shù)據(jù)類型脚仔,以及數(shù)組勤众,映射,結(jié)構(gòu)體等復雜類型鲤脏。數(shù)組们颜,映射,結(jié)構(gòu)體也支持自定義的結(jié)構(gòu)體猎醇。我們來看一個自定義結(jié)構(gòu)體的定義:
pragma solidity ^0.4.0;
contract SimpleStruct{
//學生
struct Student{
string name;
int num;
}
//班級
struct Class{
string clsName;
//學生的列表
Student[] students;
mapping(string=>Student)index;
}
}
在上面的代碼中窥突,我們定義了一個簡單的結(jié)構(gòu)體Student
,它包含一些基本的數(shù)據(jù)類型硫嘶。另外我們還定義了一個稍微復雜一點的結(jié)構(gòu)體Class
阻问,它包含了其它結(jié)構(gòu)體Student
,以及數(shù)組沦疾,映射等類型称近。
數(shù)組類型的students
和映射類型的index
的聲明中還使用了結(jié)構(gòu)體。
1.1 結(jié)構(gòu)體定義的限制
我們不能在結(jié)構(gòu)中定義一個自己作為類型哮塞,這樣限制原因是刨秆,自定義類型的大小不允許是無限的。我們來看看下述的代碼:
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.
。雖然如此邻眷,但我們?nèi)匀荒茉陬愋蛢?nèi)用數(shù)組眠屎,映射來引用當前定義的類型,如變量mappingMemberOfOwn
肆饶,arrayMemberOfOwn
所示改衩。
2. 初始化
下面我們來說說結(jié)構(gòu)體的初始化。
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]);
}
}
上面的代碼中,我們按定義依次填入值橄镜,即可完成了初始化偎快。需要注意的是,參數(shù)要與定義的數(shù)量匹配洽胶。當你填的參數(shù)與預計初始化的參數(shù)不一致時晒夹,會提示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的命名參數(shù)初始化的方式读跷,通過傳入?yún)?shù)名和對應(yīng)值的對象。這樣做的好處在于可以不按定義的順序傳入值禾唁。我們來看看下面的例子:
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";
//按命名參數(shù)的方式進行初始化
Student memory s = Student({
age : 10,
name : "Jack",
cources: crs
});
return (s.name, s.age, s.cources[0]);
}
}
上面的例子中效览,通過在參數(shù)對象中,指定鍵為對應(yīng)的參數(shù)名蟀俊,值為你想要初化的值钦铺,我們即完成了初始化。同樣需要注意的是肢预,參數(shù)要與定義的個數(shù)一致矛洞,否則會報類似這樣的錯誤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 結(jié)構(gòu)體中映射的初始化
由于映射是一種特殊的數(shù)據(jù)結(jié)構(gòu)[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
的狀態(tài)變量storageVar
辫红,完成了映射類型的存儲空間分配。然后我們就能對映射類型賦值了祝辣。
如果你嘗試對memory
的映射類型賦值贴妻,會報錯Error: Member "map" is not available in struct StructMappingInitial.A memory outside of storage.
。
3. 結(jié)構(gòu)體的可見性
關(guān)于可見性蝙斜,當前只支持internal
的名惩,后續(xù)不排除放開這個限制。詳見開發(fā)者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 繼承中使用
結(jié)構(gòu)體由于是不對外可見的孕荠,所以你只可以在當前合約娩鹉,或合約的子類中使用攻谁。包含自定義結(jié)構(gòu)體的函數(shù)均需要聲明為internal
的。
pragma solidity ^0.4.0;
contract A{
struct S{
string para1;
int para2;
}
function f(S s) internal{
//...
}
function f1(){
//當前類中使用結(jié)構(gòu)體
S memory s = S("Test", 10);
f(s);
}
}
contract B is A{
function g(){
//字類中使用結(jié)構(gòu)體
S memory s = S("Test", 10);
//調(diào)用父類方法
f(s);
}
}
在上面的代碼中弯予,我們聲明了f(S s)
戚宦,由于它包含了struct
的S
,所以不對外可見熙涤,需要標記為internal
阁苞。你可以在當前類中使用它,如f1()
所示祠挫,你還可以在子類中使用函數(shù)和結(jié)構(gòu)體,如B
合約的g()
方法所示悼沿。
4. 跨合約的臨時解決方案
結(jié)構(gòu)體等舔,由于是動態(tài)內(nèi)容。當前不支持在多個合約間互用糟趾,目前一種臨時的方案如下[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){
//你要實現(xiàn)的內(nèi)容
}
}
在上面的例子中,我們手動將要返回的結(jié)構(gòu)體拆解為基本類型進行了返回义郑。
關(guān)于作者
專注基于以太坊(Ethereum)的相關(guān)區(qū)塊鏈(Blockchain)技術(shù)蝶柿,了解以太坊,Solidity非驮,Truffle交汤,web3.js。
個人博客: http://me.tryblockchain.org
版權(quán)所有劫笙,轉(zhuǎn)載注明出處
參考資料
-
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 ?