什么是元组
普通的数组返回方式
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);
}
}