Skip to main content

The expression using Library for type allows us to use a syntaxsyntactic sugar when using certain functions forfrom the library. It is not required, but it makemakes the contract more readable.

For example, SafeMath had

library SafeMath {
    function add(uint256 a, uint256 b) public pure returns (uint256) {
        // some unimportant details here
    }
}

Then you could use in a contract

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns add(x, y);
}

You can add using SafeMath for uint256

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns x.add(y);
}

It isn't much per se, but it has a syntax close to calling a methodsmethod from an object.

The expression using Library for type allows to use a syntax sugar when using certain functions for the library. It is not required, but it make the contract more readable.

For example SafeMath had

library SafeMath {
    function add(uint256 a, uint256 b) public pure returns (uint256) {
        // some unimportant details here
    }
}

Then you could use in a contract

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns add(x, y);
}

You can add using SafeMath for uint256

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns x.add(y);
}

It isn't much per se, but it has a syntax close to calling a methods from an object.

The expression using Library for type allows us to use a syntactic sugar when using certain functions from the library. It is not required, but it makes the contract more readable.

For example, SafeMath had

library SafeMath {
    function add(uint256 a, uint256 b) public pure returns (uint256) {
        // some unimportant details here
    }
}

Then you could use in a contract

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns add(x, y);
}

You can add using SafeMath for uint256

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns x.add(y);
}

It isn't much per se, but it has a syntax close to calling a method from an object.

Source Link
Ismael
  • 30.3k
  • 23
  • 54
  • 97

The expression using Library for type allows to use a syntax sugar when using certain functions for the library. It is not required, but it make the contract more readable.

For example SafeMath had

library SafeMath {
    function add(uint256 a, uint256 b) public pure returns (uint256) {
        // some unimportant details here
    }
}

Then you could use in a contract

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns add(x, y);
}

You can add using SafeMath for uint256

function (uint256 x, uint256 y) public pure returns (uint256) {
    returns x.add(y);
}

It isn't much per se, but it has a syntax close to calling a methods from an object.