solidity icon indicating copy to clipboard operation
solidity copied to clipboard

Allow declaration of multiple variables in first clause of `for` statement

Open lukehutch opened this issue 3 years ago • 3 comments
trafficstars

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.

lukehutch avatar Jun 25 '22 18:06 lukehutch

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).

ekpyron avatar Aug 12 '22 14:08 ekpyron

Yes, this is exactly what I need.

@ekpyron @nikola-matic

emretepedev avatar Sep 17 '22 20:09 emretepedev

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.

ekpyron avatar Sep 18 '22 19:09 ekpyron

it is not purely cosmetics, it prevents scope leakage

duralsh avatar Oct 13 '22 12:10 duralsh

This issue has been marked as stale due to inactivity for the last 90 days. It will be automatically closed in 7 days.

github-actions[bot] avatar Mar 30 '23 12:03 github-actions[bot]

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.

github-actions[bot] avatar Apr 07 '23 12:04 github-actions[bot]