什麼是元組
普通的數組返回方式
pragma solidity ^0.4.4;
contract C{
uint[] data = [1,2,3,4,5];
function f() constant returns (uint[]){
return data;
}
}
普通的字典返回value值方式,但是如何返回一個字典樣式的數據呢?
pragma solidity ^0.4.4;
contract C{
mapping (uint => string) public students;
function C(){
students[0] = "wt";
students[1] = "wtTwelveRngs";
}
function students(uint id) constant returns (string){
return students[id];
}
}
使用元組的方式
pragma solidity ^0.4.4;
contract C{
mapping (uint => string) students;
function C(){
students[0] = "wt";
students[1] = "wtTwelveRngs";
}
function students1() constant returns (string name0,string name1){
name0 = students[0];
name1 = students[1];
}
//students1等價於students2
function students2() constant returns (string name0,string name1){
name0 = students[0];
name1 = students[1];
return (name0,name1);
}
//元組,直接返回多種數據類型
function f() constant returns (uint,bool,string){
return (101,true,"wt");
}
}
元組的使用
在聲明方法時,如果寫了確定的返回值時,可以省略return
pragma solidity ^0.4.4;
contract Simple{
function arithmentics(uint _a,uint _b)constant returns(uint,uint){
return (_a +_b,_a*_b);
}
function arithmentics1(uint _a,uint _b)constant returns(uint o_sum,uint o_product){
o_sum = _a + _b;
o_product = _a + _b;
}
}
pragma solidity ^0.4.4;
contract Simple{
function f() constant returns(uint,bool,uint){
return(7,true,2);
}
function g1() constant returns(uint,bool,uint){
var (x,b,y) = f();
return(x,b,y);
}
function g2() constant returns(uint,uint){
var (x,b,y) = f();
(x,y) = (2,7);
return (x,y);
}
}