interface_libraries.rst 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. Interfaces and libraries
  2. ========================
  3. Interfaces
  4. __________
  5. An interface is a contract sugar type with restrictions. This type cannot be instantiated; it can only define the
  6. functions prototypes for a contract. This is useful as a generic interface.
  7. .. code-block:: solidity
  8. interface operator {
  9. function op1(int32 a, int32 b) external returns (int32);
  10. function op2(int32 a, int32 b) external returns (int32);
  11. }
  12. contract ferqu {
  13. operator op;
  14. constructor(bool do_adds) {
  15. if (do_adds) {
  16. op = new m1();
  17. } else {
  18. op = new m2();
  19. }
  20. }
  21. function x(int32 b) public returns (int32) {
  22. return op.op1(102, b);
  23. }
  24. }
  25. contract m1 is operator {
  26. function op1(int32 a, int32 b) public override returns (int32) {
  27. return a + b;
  28. }
  29. function op2(int32 a, int32 b) public override returns (int32) {
  30. return a - b;
  31. }
  32. }
  33. contract m2 is operator {
  34. function op1(int32 a, int32 b) public override returns (int32) {
  35. return a * b;
  36. }
  37. function op2(int32 a, int32 b) public override returns (int32) {
  38. return a / b;
  39. }
  40. }
  41. - Interfaces can only have other interfaces as a base contract
  42. - All functions must the ``external`` visibilty
  43. - No constructor can be declared
  44. - No contract storage variables can exist (however constants are allowed)
  45. - No function can have a body or implementation
  46. Libraries
  47. _________
  48. Libraries are a special type of contract which can be reused in multiple contracts. Functions declared in a library can
  49. be called with the ``library.function()`` syntax. When the library has been imported or declared, any contract
  50. can use its functions simply by using its name.
  51. .. code-block:: javascript
  52. contract test {
  53. function foo(uint64 x) public pure returns (uint64) {
  54. return ints.max(x, 65536);
  55. }
  56. }
  57. library ints {
  58. function max(uint64 a, uint64 b) public pure returns (uint64) {
  59. return a > b ? a : b;
  60. }
  61. }
  62. When writing libraries there are restrictions compared to contracts:
  63. - A library cannot have constructors, fallback or receive function
  64. - A library cannot have base contracts
  65. - A library cannot be a base contract
  66. - A library cannot have virtual or override functions
  67. - A library cannot have payable functions
  68. .. note::
  69. When using the Ethereum Foundation Solidity compiler, library are a special contract type and libraries are
  70. called using `delegatecall`. Parity Substrate has no ``delegatecall`` functionality so Solang statically
  71. links the library calls into your contract code. This does make for larger contract code, however this
  72. reduces the call overhead and make it possible to do compiler optimizations across library and contract code.
  73. Library Using For
  74. _________________
  75. Libraries can be used as method calls on variables. The type of the variable needs to be bound to the
  76. library, and the type of the first parameter of the function of the library must match the type of a
  77. variable.
  78. .. code-block:: solidity
  79. contract test {
  80. using lib for int32[100];
  81. int32[100] bar;
  82. function foo() public returns (int64) {
  83. bar.set(10, 571);
  84. }
  85. }
  86. library lib {
  87. function set(int32[100] storage a, uint index, int32 val) internal {
  88. a[index] = val;
  89. }
  90. }
  91. The syntax ``using`` `library` ``for`` `Type` ``;`` is the syntax that binds the library to the type. This
  92. must be specified on the contract. This binds library ``lib`` to any variable with type ``int32[100]``.
  93. As a result of this, any method call on a variable of type ``int32[100]`` will be matched to library ``lib``.
  94. For the call to match, the first argument of the function must match the variable; note that here, `bar`
  95. is of type ``storage``, since all contract variables are implicitly ``storage``.
  96. There is an alternative syntax ``using`` `library` ``for *;`` which binds the library functions to any
  97. variable that will match according to these rules.