xxxxxxxxxx
// `for` is used to attach functions to variable types.
// Example:
function sum(uint a, uint b) returns(uint) {
return a + b;
}
using sum for uint;
// then you can use this inside your contracts:
contract MyContract {
function add1(uint num) public pure returns(uint) {
return num.sum(1);
// `num` is passed as the first parameter (`a`) and `1` as the second (`b`)
}
}
// You can also use libraries this way:
library Math {
function sum(uint a, uint b) returns(uint) {
return a + b;
}
function sub1(uint x) returns(uint) {
return(x - 1);
}
}
contract MyContract {
using Math for uint;
function uselessCalculation(uint x, uint y) returns(uint) {
return x.sum(y).sub1();
}
}
xxxxxxxxxx
To access a member (like a state variable) of the current contract, you do not typically add the this. prefix, you just access it directly via its name. Unlike in some other languages, omitting it is not just a matter of style, it results in a completely different way to access the member, but more on this later.