using for solidity

The solution for “using for solidity” can be found here. The following code will assist you in solving the problem.

// `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();
}

}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

Similar Posts