solidity
solidity copied to clipboard
Allow declaration of multiple variables in first clause of `for` statement
Abstract
Currently it is impossible to declare more than one variable in the first clause of a for statement, e.g.
for (uint256 i = 0, len = array.length; i < len; ++i) { ... }
Motivation
The above pattern saves gas, while saving polluting the namespace of the currently-possible alternative:
uint256 len = array.length;
for (uint256 i = 0; i < len; ++i) { ... }
Specification
Please allow multiple declarations of variables of the same type in the first clause of a for-loop, as is possible in C/C++/Java.
Backwards Compatibility
No issues, since this is a superset of the current functionality.
This is worth a thought, but I'd not assign it high priority (unfortunately, we have a rather large backlog of non-critical design decisions).
Yes, this is exactly what I need.
@ekpyron @nikola-matic
Note that this has no effect on stack too deep errors whatsoever. (If you experience those, have you tried optimized via-ir code generation?)
Also note that none of this will save any gas either, this is purely cosmetics.
it is not purely cosmetics, it prevents scope leakage
This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.
Hi everyone! This issue has been automatically closed due to inactivity. If you think this issue is still relevant in the latest Solidity version and you have something to contribute, feel free to reopen. However, unless the issue is a concrete proposal that can be implemented, we recommend starting a language discussion on the forum instead.