using.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ``Using`` directive
  2. ===================
  3. Binding methods to types with ``using``
  4. ---------------------------------------
  5. Methods can be bound to builtin types and any user-defined types like structs
  6. using the ``using`` syntax. This can be done either using libraries or free
  7. standing functions.
  8. ``using`` with free standing functions
  9. ______________________________________
  10. First, declare a function with one or more arguments. Once the function
  11. is bound with ``using``, it can be called like a method.
  12. .. code-block:: javascript
  13. function mask(uint v, uint bits) returns (uint) {
  14. return v & ((1 << bits) - 1);
  15. }
  16. function odd(uint v) returns (bool) {
  17. return (v & 1) != 0;
  18. }
  19. contract c {
  20. using {mask, odd} for *;
  21. int v;
  22. function set_v(int n) public {
  23. v = n.mask(16);
  24. }
  25. }
  26. The ``using`` declaration can be done on file scope. In this case, the type must
  27. be specified in place of ``*``. The first argument must match the type that is
  28. be used in the ``using`` declaration.
  29. If a user-defined type is used, the the ``global`` keyword can be used. This
  30. means the ``using`` binding can be used in any file, even when the type is
  31. imported.
  32. .. code-block:: solidity
  33. struct User {
  34. string name;
  35. uint count;
  36. }
  37. function clear_count(User memory user) {
  38. user.count = 0;
  39. }
  40. using {clear_count} for User global;
  41. Now even when ``User`` is imported, the clear_count() method can be used.
  42. .. code-block:: solidity
  43. import {User} from "user.sol";
  44. contract c {
  45. function foo(User memory user) {
  46. user.clear_count();
  47. }
  48. }
  49. ``using`` with libraries
  50. ________________________
  51. A library may be used for handling methods on a type. First, declare a library
  52. with all the methods you want for a type, and then bind the library to the type
  53. with ``using``.
  54. .. code-block:: solidity
  55. struct User {
  56. string name;
  57. uint name;
  58. }
  59. library UserLibrary {
  60. function clear_count(User user) internal {
  61. user.count = 0;
  62. }
  63. function inc(User user) internal {
  64. user.count++;
  65. }
  66. function dec(User user) internal {
  67. require(user.count > 0);
  68. user.count--;
  69. }
  70. }
  71. using UserLibrary for User global;
  72. Scope for ``using``
  73. ___________________
  74. The ``using`` declaration may be scoped in various ways:
  75. - Globally by adding the ``global`` keyword. This means the methods are available
  76. in any file.
  77. - Per file, by omitting the ``global`` keyword
  78. - Per contract, by putting the ``using`` declaration in a contract definition
  79. If the scope is per contract, then the type maybe be replaced with ``*`` and
  80. the type from the first argument of the function will be used.