feat: add AutoIncrementing contract#1023
Conversation
shrugs
commented
Jun 17, 2018
|
I feel like this would be better as a library with a struct, so that a contract could have more than one auto-incrementing number. Structs don't have private fields though, so that would blow out the nice encapsulation, but I don't think it's too big a deal. |
|
@frangio can you explain in more detail what you mean? |
|
Yes, sorry for the terseness. 🙇♂️ What I wanted to say is that implementing this functionality by inheritance doesn't feel right to me. The main reason why I feel this way is that it only allows for one auto-incrementing counter per contract. Conceptually, "auto-incrementing counter" is a new datatype, with an operation "next", so I would expect to be able to create several instances of it. Solidity allows the creation of new datatypes using libraries and structs. In this case it would be something like: struct Counter {
uint256 _prev;
}
function next(Counter counter) internal returns (uint256);Used as: Counter private ids;
...
uint256 newId = ids.next();
This enables having multiple counters and doesn't add to the inheritance graph of a contract. On the other hand, whereas the contract implementation in this PR is nicely encapsulated by having the I think Solidity should have Thoughts? |
3b5bb7f to
b18cede
Compare
|
@frangio I've update the contract using that spec, and I like it better! Can you give it a last review? |
frangio
left a comment
There was a problem hiding this comment.
Looks nice!
I'd prefer to keep the linting changes on unrelated files out of this PR.
| * | ||
| * Include with `using AutoIncrementing for AutoIncrementing.Counter;` | ||
| * @notice Does not allow an Id of 0, which is popularly used to signify a null state in solidity. | ||
| * Does not protect from overflows, but if you have 2^256 ids, you have other problems. |
There was a problem hiding this comment.
LOL. It's actually physically impossible (as in not enough energy in the universe-impossible) to overflow a counter like this. This line is funny though, maybe we can just add in parenthesis that for reals it will never overflow so that people don't worry unnecessarily.
|
fixed! |
0e8f8ce to
df26174
Compare