Solidity theorytheory 0/50 · 0%
Structure · easy

20. Libraries and `using ... for`

Reusable pure logic and attached functions.

A `library` holds reusable functions. Internal library functions are inlined into your bytecode; external ones live at their own address and are reached by `delegatecall`, so they run in your storage context.

library Math {
    function min(uint256 a, uint256 b) internal pure returns (uint256) {
        return a < b ? a : b;
    }
}

using Math for uint256;
uint256 m = x.min(y);

Libraries cannot hold state, receive ETH or be inherited.

Check your understanding

  1. 1. Can a library hold state variables?

  2. 2. How are internal library functions included?

  3. 3. What does `using Math for uint256` enable?