solidity/test/libsolidity/semanticTests/operators/userDefined/unchecked_loop_counter.sol
2023-01-25 00:30:03 +01:00

32 lines
817 B
Solidity

type UncheckedCounter is uint;
using {
add as +,
lt as <
} for UncheckedCounter;
UncheckedCounter constant ONE = UncheckedCounter.wrap(1);
function add(UncheckedCounter x, UncheckedCounter y) pure returns (UncheckedCounter) {
unchecked {
return UncheckedCounter.wrap(UncheckedCounter.unwrap(x) + UncheckedCounter.unwrap(y));
}
}
function lt(UncheckedCounter x, UncheckedCounter y) pure returns (bool) {
return UncheckedCounter.unwrap(x) < UncheckedCounter.unwrap(y);
}
contract C {
uint internalCounter = 12;
function testCounter() public returns (uint) {
for (UncheckedCounter i = UncheckedCounter.wrap(12); i < UncheckedCounter.wrap(20); i = i + ONE) {
++internalCounter;
}
return internalCounter;
}
}
// ----
// testCounter() -> 20