When you're using a library like SafeMath, and then have using SafeMath for uint256; within a contract, how do uints know to become the a parameter of the library functions? Is that just a convention that applies to all functions whenever you're using a library for a data type?

For instance, you'd do some_uint.sub(4), but the sub method has both a and b parameters:

function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    assert(b <= a);
    return a - b;

Yes, the value you apply the sub method to automatically becomes the first parameter.

| improve this answer | |
  • Do you know if that's the general rule when creating library functions that are used to extend data types? (That is, that the first parameter always becomes the first argument?) Couldn't find this in the docs. – will_durant Oct 19 '17 at 3:34
  • Yes, if you extend data types with library functions, the value will always be the first param. – Nikita Fuchs Oct 19 '17 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.