RE: RE: Solidity开发指南
You are viewing a single comment's thread from:

RE: Solidity开发指南

Words
20
Reading
1 min
Listen
Play
4M

library, 标准的函数,可以反复使用。调用时类似delegatecall

// SPDX-License-Identifier: MIT  
pragma solidity ^0.8.20;
library mathlib {
  function plus(uint a, uint b) public pure returns (uint) {
    uint c = a + b;
    assert(c>=a && c>=b);
    return c;
  }
}

// SPDX-License-Identifier: MIT 
pragma solidity ^0.8.20;
import "./mathlib.sol";
contract testLib {
    using mathlib for uint;
    function add (uint x, uint y) public pure returns (uint) {
        return x.plus(y);
        // return mathlib.plus(x,y);
    }
}

//使用有两种方法:
使mathlib.plus(x,y)

  using mathlib for uint;
  //using mathlib for uint[]
  x.plus(y);
@lemooljiang: library, | Ecency