0

When we use the Counters library, we init it usually as such

 using Counters for Counters.Counter;
 Counters.Counter private _tokenIds;

so far all good. Using Counters library methods for Counters.Counter (the struct in the library) and assigning _tokenIds to point to that struct. (+-? cool.)

What confuses me is the function definitions inside Counters; i.e

function current(Counter storage counter) internal view returns (uint256) {
  return counter._value;
}

function increment(Counter storage counter) internal {
  unchecked {
    counter._value += 1;
  }
}

The function takes in a varaible called counter ? is it not expecting an argument ? Where is the link between our defined _tokenIds to the smaller-case counter ? I don't know why I find this so confusing but it seems like something's missing to me (even tho I know its not missing, just failing to understand).

Thanks in advance.

1 Answers1

2

The using <library> for <type> expression allows you to use functions of the library on variables of this type. And it automatically passes the variable as the first argument of the function when you're calling it as a member function.

So in your case, Counters.current(_tokenIds) (library function) is the same as _tokenIds.current() (member function).

Docs: https://docs.soliditylang.org/en/v0.8.14/contracts.html#using-for

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100