3

I am using two different ways to get the same result and wondering what the real difference is. Both options (library, using, for and contract, is)deliver just an extended code from a file.

Method no 1

library.sol:

pragma solidity ^0.4.19;

contract IntExtended {

    function increment(uint _self) returns (uint) {
        return _self+1;
    }
}

contract_is_method.sol:

pragma solidity ^0.4.19;

import "browser/library.sol";

contract TestLibrary is IntExtended {

    function testIncrement(uint _base) returns (uint) {
        return IntExtended.increment(_base);
    }       
}

Method no 2

library.sol:

pragma solidity ^0.4.19;

library IntExtended {

    function increment(uint _self) returns (uint) {
        return _self+1;
    }
}

using_for_method.sol:

pragma solidity ^0.4.19;

import "library.sol";

contract TestLibrary {
    using IntExtended for uint;

    function testIncrement(uint _base) returns (uint) {
        return IntExtended.increment(_base);
    }
}
4

In the first instance your contract is inheriting from the TestLibrary contract, and therefore you can perform the operation with increment(_base) or super.increment(_base). Additionally, if the function is public (which it will be by default), then that function is viewable and usable on your contract when it is deployed.

In the second instance, you are applying the library to the uint type instead. This allows you to perform your operation by calling _base.increment(). Note that you don't need to supply the number as the first argument to such a function since it will default to the object you're calling the method on. Additionally, since the library is applied to your uint, it's not a method of your contract and can't be called from outside.

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.