using.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. .. include:: ../examples/using.sol
  13. :code: solidity
  14. The ``using`` declaration can be done on file scope. In this case, the type must
  15. be specified in place of ``*``. The first argument must match the type that is
  16. be used in the ``using`` declaration.
  17. If a user-defined type is used, the the ``global`` keyword can be used. This
  18. means the ``using`` binding can be used in any file, even when the type is
  19. imported.
  20. .. include:: ../examples/using_global.sol
  21. :code: solidity
  22. Now even when ``User`` is imported, the clear_count() method can be used.
  23. .. include:: ../examples/using_imports.sol
  24. :code: solidity
  25. .. _user_defined_operators:
  26. User defined Operators
  27. ______________________
  28. The ``using`` directive can be used to bind operators for :ref:`user defined types <user_defined_types>`
  29. to functions. A binding can be set for the operators: ``==``, ``!=``, ``>=``, ``>``, ``<=``, ``<``, ``~``,
  30. ``&``, ``|``, ``^``, ``-`` (both negate and subtract), ``+``, ``*``, ``/``, and ``%``.
  31. First, declare a function with the correct prototype that implements the operator.
  32. * The function must be free standing: declared outside a contract.
  33. * The function must have ``pure`` mutability.
  34. * All the parameters must be the same user type.
  35. * The number of arguments depends on which operator is implemented; binary operators require two and unary operators, one.
  36. * The function must return either ``bool`` for the comparison operators, or the same user type as the parameters for the other operators.
  37. Then, bind the function to the operator using the syntax ``using {function-name as operator} for user-type global;``.
  38. Operators can only be defined with ``global`` set. Note that the ``-`` operator is
  39. used for two operators: subtract and negate. In order to bind the unary negate operator,
  40. the function must have a single parameter. For the subtract operator, two parameters are required.
  41. .. include:: ../examples/user_defined_operators.sol
  42. :code: solidity
  43. ``using`` with libraries
  44. ________________________
  45. A library may be used for handling methods on a type. First, declare a library
  46. with all the methods you want for a type, and then bind the library to the type
  47. with ``using``.
  48. .. code-block:: solidity
  49. struct User {
  50. string name;
  51. uint name;
  52. }
  53. library UserLibrary {
  54. function clear_count(User user) internal {
  55. user.count = 0;
  56. }
  57. function inc(User user) internal {
  58. user.count++;
  59. }
  60. function dec(User user) internal {
  61. require(user.count > 0);
  62. user.count--;
  63. }
  64. }
  65. using UserLibrary for User global;
  66. Scope for ``using``
  67. ___________________
  68. The ``using`` declaration may be scoped in various ways:
  69. - Globally by adding the ``global`` keyword. This means the methods are available
  70. in any file.
  71. - Per file, by omitting the ``global`` keyword
  72. - Per contract, by putting the ``using`` declaration in a contract definition
  73. If the scope is per contract, then the type maybe be replaced with ``*`` and
  74. the type from the first argument of the function will be used.